UnitedForums - UK Web Hosting Forum UnitedHosting Community Hosting Forums
Network and Server StatusCustomer SupportUK Web Hosting
UnitedHostingUnitedHosting Sitemap UK Hosting ForumUK Web HostingWeb Hosting ForumsUK Reseller HostingWeb Host CommunityUK Managed Dedicated ServersHosting Help and SupportUK Domain Name Registration

Go Back   UnitedForums.co.uk > UnitedHosting Community > Website Development & Scripting

Reply
 
Thread Tools Rate Thread Display Modes
Old 8th January 2008, 10:03 AM   #1 (permalink)
xyz
Registered User
 
Join Date: Mar 2006
Posts: 33
Question PHP and Checkbox Values

I'm probably missing something obvious but here goes:

If I have a HTML checkbox field on a php form, for example:

<input type="checkbox" id="chkField" name="chkField" value="Red">Red
<input type="checkbox" id="chkField" name="chkField" value="Blue">Blue

If I select both Red and Blue, then submit my form it only seems to store the last value selected and not both.

Do I have to do something to handle this or am I missing something.

Any help appreciated.
xyz is offline   Reply With Quote
Old 8th January 2008, 12:28 PM   #2 (permalink)
Euge
Registered User
 
Euge's Avatar
 
Join Date: Feb 2003
Location: Lost in confusion
Posts: 700
Checkboxes are handled as arrays. Change the name attribute to "chkField[]" and see if that works.
Euge is offline   Reply With Quote
Old 8th January 2008, 12:39 PM   #3 (permalink)
TygerTyger
Lumberjack and OK
 
Join Date: Aug 2004
Posts: 833
Or just give them different names, I always do because it's so much easier to keep track of. Change the IDs too, they must all have different names.

Code:
<input type="checkbox" id="Red" name="Red" value="Red"><label for="Red">Red</label>
<input type="checkbox" id="Blue" name="Blue" value="Blue"><label for="Blue">Blue</label>
$_POST['Red'] will equal 'on' if it is checked, $_POST['Blue'] will equal 'on' if it is checked, etc. You can indulge in a bit of accessibility while you're there by using <label for="elementID"> for the labels.

Last edited by TygerTyger : 8th January 2008 at 12:41 PM.
TygerTyger is offline   Reply With Quote
Old 8th January 2008, 02:38 PM   #4 (permalink)
xyz
Registered User
 
Join Date: Mar 2006
Posts: 33
Thanks - one more question please

Thanks both, I have opted for Euge's suggestion (hadn't thought of the id being an array) as I am dynamically generating a form and therefore do not know how many fields of checkbox type there will be.

One further question if I may. So I know have the following:

<input type="checkbox" id="chkField[]" name="chkField[]" value="Red">Red
<input type="checkbox" id="chkField[]" name="chkField[]" value="Blue">Blue

and using the following PHP to retrieve the checkbox values from my array:

while ( list ( $key, $val ) = @each ( $chkField )) {
echo $val;
}

THIS WORKS FINE - because $chkField is an array

However, the field ID (id="chkField[]") is being generated dynamically from an array of fields - $v_FIELD_LIST and I am therefore looping through these fields, creating the fields within a foreach loop:

foreach ($v_FIELD_LIST as $value) {

}

so where $value will have the field name, it is not an array and therefore:

while ( list ( $key, $val ) = @each ( $value )) {
echo $val;
}

DOESN'T WORK

My question is: how do I reference my array of chkField and not the field name chkField.

Hope this makes sense.
xyz is offline   Reply With Quote
Old 8th January 2008, 06:31 PM   #5 (permalink)
Simon
Dedicated to life!
 
Simon's Avatar
 
Join Date: Jul 2005
Location: 36°38'4.48"N - 4°42'18.52"W
Posts: 2,058
Send a message via MSN to Simon Send a message via Yahoo to Simon Send a message via Skype™ to Simon
If I understand correctly you should be able to substitute @each ( $value ) with

PHP Code:
@each ( $$value 
notice the double dollar..
__________________
Freelance PHP Programming
__________________
Simon is offline   Reply With Quote
Old 8th January 2008, 08:41 PM   #6 (permalink)
xyz
Registered User
 
Join Date: Mar 2006
Posts: 33
Excellent

Simon, Excellent, that's exactly it.

Thanks.
xyz is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 02:19 PM.

UK Web Hosting  |  UK Reseller Hosting  |  UK Dedicated Servers UnitedHosting  |  UnitedSupport  |  UnitedForums  |  SEO by vBSEO 3.0.0
Copyright © 1998-2008 United Communications Limited. All Rights Reserved. Registered in England and Wales 3651923 - VAT Reg No. 737662309