setParam('role', $role);$user->setParam('subject', $subject);$user->save();?>Below is the html code for the checkboxes: Subject areas(please check all that apply): Elementary Literarcy Elementary Math Secondary English Secondary Math Secondary History and Social Science Secondary Science Music Art PE Special Education Bilingual World Languages Agriculture other  How do I get all the user's selected values from the checkboxes be shown inside the params? The end result should look like this inside the params if the user selected Music and Art as his subject areas:subject = Music, ArtThanks,mov2046"> More than 1 Value from Checkboxes Inserted into "params" - Forums

Forums

More than 1 Value from Checkboxes Inserted into "params"

mov2046 25 Jan, 2011
Hi,

I extended my Joomla registration form and added some checkboxes for a question (multiple selections are allowed), using the purchased tutorial on "Build an Extended Userform." All the newly created fields' data go into the jos_users' params. The text field's and radio field's values got inserted into the params just fine. However, regarding the checkboxes, if a user selects more than one checkbox, only the first piece of data gets inserted into the params.
Other checked items are not inserted into the table. Below are the php codes that I used inside the On Submit Code-after sending email box. "subject" is the name of the checkboxes. "position" is the text field. "role" is a radio input.

<?php

$user_id = JRequest::getInt('cf_user_id', 0, 'post');
r
$user =& JFactory::getUser($user_id);


$position = JRequest::getString('position', '', 'post');
$role = JRequest::getString('role', '', 'post');
$subject = JRequest::getString('subject', '', 'post');




$user->setParam('position', $position);
$user->setParam('role', $role);
$user->setParam('subject', $subject);

$user->save();
?>




Below is the html code for the checkboxes:

<div class="form_item">
  <div class="form_element cf_checkbox">
    <label class="cf_label">Subject areas(please check all that apply):</label>
    <br />
    <br />
    <input value="Elementary Literarcy" title="" class="radio" id="check10" name="subject" type="checkbox" />
    <label for="check10" class="check_label">Elementary Literarcy</label>
    <br />
    <input value="Elementary Math" title="" class="radio" id="check11" name="subject" type="checkbox" />
    <label for="check11" class="check_label">Elementary Math</label>
    <br />
    <input value="Secondary English" title="" class="radio" id="check12" name="subject" type="checkbox" />
    <label for="check12" class="check_label">Secondary English</label>
    <br />
    <input value="Secondary Math" title="" class="radio" id="check13" name="subject" type="checkbox" />
    <label for="check13" class="check_label">Secondary Math</label>
    <br />
    <input value="Secondary History and Social Science" title="" class="radio" id="check14" name="subject" type="checkbox" />
    <label for="check14" class="check_label">Secondary History and Social Science</label>
    <br />
    <input value="Secondary Science" title="" class="radio" id="check15" name="subject" type="checkbox" />
    <label for="check15" class="check_label">Secondary Science</label>
    <br />
    <input value="Music" title="" class="radio" id="check16" name="subject" type="checkbox" />
    <label for="check16" class="check_label">Music</label>
    <br />
    <input value="Art" title="" class="radio" id="check17" name="subject" type="checkbox" />
    <label for="check17" class="check_label">Art</label>
    <br />
    <input value="PE" title="" class="radio" id="check18" name="subject" type="checkbox" />
    <label for="check18" class="check_label">PE</label>
    <br />
    <input value="Special Education" title="" class="radio" id="check19" name="subject" type="checkbox" />
    <label for="check19" class="check_label">Special Education</label>
    <br />
    <input value="Bilingual" title="" class="radio" id="check110" name="subject" type="checkbox" />
    <label for="check110" class="check_label">Bilingual</label>
    <br />
    <input value="World Languages" title="" class="radio" id="check111" name="subject" type="checkbox" />
    <label for="check111" class="check_label">World Languages</label>
    <br />
    <input value="Agriculture" title="" class="radio" id="check112" name="subject" type="checkbox" />
    <label for="check112" class="check_label">Agriculture</label>
    <br />
    <label>other
      <input name="other" type="text" id="other" value="" size="30" maxlength="45">
    </label>
    <br />
  </div>
  <div class="cfclear"> </div>
</div>


How do I get all the user's selected values from the checkboxes be shown inside the params? The end result should look like this inside the params if the user selected Music and Art as his subject areas:

subject = Music, Art


Thanks,
mov2046
Max_admin 27 Jan, 2011
Hi,

For multi selection fields, the field name should end with "[]" and in the code you will need to change it alittle bit to be:

//field name="subject[]"
$subject = JRequest::getString('subject', array(), 'post');
$subject = implode(",", $subject);


Cheers,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
mov2046 28 Jan, 2011
Hi Max,

I inserted the code below to On Submit code - after sending email:

//field name="subject[]"
$subject = JRequest::getString('subject', array(), 'post');
$subject = implode(",", $subject);



I got this error message after I submitted the form:

Warning: implode() [function.implode]: Invalid arguments passed in /home/content/s/p/i/spicetrader/html/joomlasciexp/components/com_chronocontact/libraries/customcode.php(51) : eval()'d code on line 18


Values were not inserted to the params.

If you could reply asap, I'd truly appreciate it.

Thanks,
mov2046
GreyHead 30 Jan, 2011
Hi mov2046,

To get an array result you can't use getString(). I think the line needs to be
$subject = JRequest::getVar('subject', array(), 'post', 'array');
Please check and see if that works OK.

Bob
mov2046 30 Jan, 2011
Hi Bob,

I replaced my codes with yours and didn't get any error message anymore. Please see codes below. However, only the last selected value from the checkboxes got inserted into the params. Any thoughts?

Thanks,
mov2046


<?php
// get the user id saved by the plug-in
$user_id = JRequest::getInt('cf_user_id', 0, 'post');
// get the Joomla! User object for the new user
$user =& JFactory::getUser($user_id);
...
$subject = JRequest::getVar('subject', array(), 'post', 'array');
$subject = implode(",", $subject);

...
$user->setParam('subject', $subject);
// Save the user object
$user->save();
?>
GreyHead 31 Jan, 2011
Hi mov2046,

I'm a bit lost here. Getting the last result only usually means that you have several checkboxes with the same name. To get them to return an array, the names have to be like name='check0[]' - exactly the same in each checkbox in the array.

Please post a link to the form so we can take a quick look.

Bob
mov2046 31 Jan, 2011
Hi Bob,

I just sent you the url via PM.

Thanks,
mov2046
mov2046 31 Jan, 2011
Bob,

I got it to work. Never mind the previous post. I changed name="subject" to name="subject[]" Thanks again.

mov2046
GreyHead 01 Feb, 2011
Hi mov2046,

Great, thanks for letting me know.

Bob
Fireflight 04 Nov, 2011
Thanks. This solved the same last item selected issue for me as well.

No dashes in name attributes, need []'s for check/radio groups. Chronoforms needs a quick cheatsheet for all those little details.
This topic is locked and no more replies can be posted.