Checkboxes in User Profile from Accounts

14 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: May 10, 2011   (RSS)

By Toledoh - January 25, 2011

Hi Guys.

In the readme.txt with the membership plugin, it shows how to display radio boxes from the admin in the edit profile page.

I need the same thing to happen but with checkboxes (multi value).

I thought changing type="radio" to type="checkbox", and ensuring the field was correct in the admin would work... but no.

Can you help?
Cheers,

Tim (toledoh.com.au)
Attachments:

profile_003.php 10K

Re: [Toledoh] Checkboxes in User Profile from Accounts

By Jason - January 25, 2011

Hi,

checkedIf() only works when you can only have 1 value for a field (like a radio button).

We can modify the code found in the readme file slightly to get what you're looking for.

I've highlighted the changes in red:

<tr>
<td valign="top">Interests</td>
<td>
<?php $fieldname = 'interests'; ?>
<?php $fieldValues = explode("\t",trim(@$_REQUEST[$fieldname],"\t"));?>
<?php $idCounter = 0; ?>
<?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
<?php $id = "$fieldname." . ++$idCounter; ?>
<input type="checkbox" name="<?php echo $fieldname ?>" id="<?php echo $id ?>"
value="<?php echo htmlspecialchars($value) ?>" <?php if(in_array($value,$fieldValues)):?> checked="checked" <?php endif ?>/>

<label for="<?php echo $id ?>"><?php echo htmlspecialchars($value) ?></label><br/>

<?php endforeach ?>
</td>
</tr>


With the first red line, we break up our multi-select field into an array. With the second red line, we output checked="checked" if the current value is found in the array we created in the first line.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Checkboxes in User Profile from Accounts

By Toledoh - January 25, 2011

Brilliant!
Cheers,

Tim (toledoh.com.au)

Re: [Jason] Checkboxes in User Profile from Accounts

By Toledoh - January 28, 2011 - edited: January 28, 2011

Hi Jason,

I've been playing around - and now this doesn't seem to work. It's fine to display multi items when those selections have been made in the admin - however, if you update the profile via profile.php, it will only record the last checked item.

Does it matter if in the accounts table, the "interests" field is a multi-value pull down or multi-value checklist? (I would prefer checklist)

Also, I've changed

<label for="<?php echo $id ?>"><?php echo htmlspecialchars($value) ?></label> to <label for="<?php echo $id ?>"><?php echo htmlspecialchars($label) ?></label>

I've attached my profile page again...
Cheers,

Tim (toledoh.com.au)
Attachments:

profile_005.php 10K

Re: [Toledoh] Checkboxes in User Profile from Accounts

By Jason - January 28, 2011

Hi Tim,

Multi-value checklists and multi-value pull downs are both stored the same way in CMS Builder, so it won't matter which one you're using.

There are a couple of things happening here. First, we'll need to make our check boxes an array so that when we submit it, we can see multiple values. To do this, we just need to put [] after the field name like this:

<input type="checkbox" name="<?php echo $fieldname ?>[]" id="<?php echo $id ?>"
value="<?php echo htmlspecialchars($value) ?>" <?php if(in_array($value,$fieldValues)):?> checked="checked" <?php endif ?>/>


Next, where we're processing the form, we need to change the array of values submitted to a single tab separated string:

$membership = "\t".join("\t",@$_REQUEST['membership'])."\t";

This is now the value we'll use in our update statement:

membership = '".mysql_escape( $membership )."',

This should take care of your updating issue. Give this a try.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Toledoh] Checkboxes in User Profile from Accounts

By Jason - January 28, 2011

Hi Tim,

Try changing the code to this:

<?php $fieldname = 'membership'; ?>

<?php
if(is_array(@$_REQUEST[$fieldname])){
$fieldValues = $_REQUEST[$fieldname];
}
else{
$fieldValues = explode("\t",trim(@$_REQUEST[$fieldname],"\t"));
}
?>

<?php $idCounter = 0; ?>
<?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
<?php $id = "$fieldname." . ++$idCounter; ?>
<input type="checkbox" name="<?php echo $fieldname ?>[]" id="<?php echo $id ?>"
value="<?php echo htmlspecialchars($value) ?>" <?php if(in_array($value,$fieldValues)):?> checked="checked" <?php endif ?>/>
<label for="<?php echo $id ?>"><?php echo htmlspecialchars($label) ?></label><br/>

<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Checkboxes in User Profile from Accounts

By Toledoh - January 28, 2011

Great one - that's working now Jason. Thanks heaps!
Cheers,

Tim (toledoh.com.au)

Re: [Jason] Checkboxes in User Profile from Accounts

By zip222 - May 9, 2011

I am trying to setup a single "opt-in" checkbox, but I am not able to get it update the database. Here is what I have on the page...

this in the header:
email_list = '".mysql_escape( $_REQUEST['email_list'] )."',

and this in the body:
<input type="checkbox" name="email_list" value="<?php echo htmlspecialchars(@$_REQUEST['email_list']); ?>" <?php if($_REQUEST['email_list']) echo "checked='checked'" ?> /> Include me on the email list


it displays the correct status for the checkbox when the page is loaded, but if i try to change the value through the profile update page nothing changes in the database.

Re: [zip222] Checkboxes in User Profile from Accounts

By Jason - May 9, 2011

Hi,

What's happening is that your checkbox is only getting a value if a value had only previously been entered. In this case, we would always want to give the checkbox a value of 1 since that value will only be submitted if it is checked.

Try this:

<input type = "checkbox" name = "email_list" value = "1" <?php checkedIf("1", @$_REQUEST['email_list']);?> />

And in your header:

email_list = '".intval( @$_REQUEST['email_list'] )."',

That way, email_list will always have a value of either 1 (checked) or 0 (unchecked).

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/