Go Back  DVD Talk Forum > General Discussions > Tech Talk
Reload this Page >

php Form with Checkboxes

Tech Talk Discuss PC Hardware, Software, Internet and Other Technology

php Form with Checkboxes

Old 07-25-05, 01:46 PM
  #1  
DVD Talk Hero
Thread Starter
 
CRM114's Avatar
 
Join Date: Jun 2001
Posts: 42,731
Likes: 0
Received 0 Likes on 0 Posts
php Form with Checkboxes

I need to create a form with many groups of checkboxes with php. The problem is if you select more than one checkbox, only the last value gets stored in MySQL. For instance.

[] Red
[] Blue
[] Green
[] Yellow

If you check blue and yellow, only Yellow gets stored. How do people deal with this situation? I wouldn't mind storing it in a comma delimited format like

Blue, Yellow

But then I have an issue if I want to display the data back in checkboxes.

Anyone have any suggestions or any good documents to look at? Thanks.
Old 07-25-05, 04:33 PM
  #2  
DVD Talk Hall of Fame
 
Join Date: Feb 2000
Location: on the mountain
Posts: 7,849
Likes: 0
Received 0 Likes on 0 Posts
CRM, I think I am the resident PHP guy. perhaps there's more. Anyways, this isn't really a PHP problem but an HTML one.

You are getting only the last one because you are naming all the checkboxes the same thing. If you have a name="checkbox_red" and name="checkbox_blue" etc then you can check the values of each one independently.

Alternatively, if you make a select box with option MULTIPLE or something you can have people select multiple options and they will come in a comma delimited list.

hope that helps. feel free to email me thru dvdtalk.
Old 07-25-05, 08:50 PM
  #3  
DVD Talk Gold Edition
 
Join Date: Dec 1999
Posts: 2,708
Likes: 0
Received 0 Likes on 0 Posts
Another way to do it is to make them into an array. This way you can just loop over the array of values when inserting them. This explains the method I've used with checkboxes

http://us3.php.net/manual/en/languag...rnal.php#50989

I just looked again at the code and it looks like it's closer to this example

http://us3.php.net/manual/en/languag...rnal.php#34820

You set all the values up like this

HTML Code:
<input type="checkbox" name="checkstuff[]" value="0">
<input type="checkbox" name="checkstuff[]" value="1">
<input type="checkbox" name="checkstuff[]" value="2">
Then you just loop over the array of values.

Last edited by Lampei; 07-25-05 at 09:04 PM.
Old 07-25-05, 11:54 PM
  #4  
DVD Talk Hall of Fame
 
Join Date: Feb 2000
Location: on the mountain
Posts: 7,849
Likes: 0
Received 0 Likes on 0 Posts
Lampei,
How does that work? I have never used an array like that. I have done like checkstuff[1], checkstuff[2], checkstuff[3]...

if you leave the array pointer just [] what does it do? it stores all the data independently at the next available array position? so checkstuff[1] would = 1, [2] = 2 etc?
Old 07-26-05, 12:27 AM
  #5  
DVD Talk Gold Edition
 
Join Date: Dec 1999
Posts: 2,708
Likes: 0
Received 0 Likes on 0 Posts
Looks like I was mistaken (it's been a little while ) I thought it was just feature_id[], but it looks like I did it using feature_id[1], feature_id[2], feature_id[3]

PHP Code:
<input type="checkbox" name="feature_id[]" value="" 
and the insert
PHP Code:
if (count($_POST['feature_id']) > ) {
   while (list(
$key$val) = each($_POST['feature_id'])) {
    
$sql "INSERT INTO features
       (listing_id,
       feature_id)
      VALUES
       (" 
$listing_id ",
       " 
$val ")";
    
$result = @mysql_query($sql$connection) or die(mysql_error());
   }
  } 
The code without the pointer may work (I think I was rushing to build a project at the time, so I didn't get a chance to test the one without numeric values...just grabbed whatever I could find at the time that worked). I'll have to give it a try and see.
Old 07-26-05, 04:00 PM
  #6  
DVD Talk Hero
Thread Starter
 
CRM114's Avatar
 
Join Date: Jun 2001
Posts: 42,731
Likes: 0
Received 0 Likes on 0 Posts
I used the name="checkboxname[]" syntax in the form and passed it to php script which put each value into a variable separated by a comma. I insert that list in to the database. Then when its time to display the data on an update form, I used the explode() function and put the values into an array. After that, I put in a conditional into the checkbox to determine whether to mark it "checked" or not:



Works. I know its bad form to do it this way. It defeats the purpose of a database to store multiple values in a field. But I'm in a hurry.
Old 07-27-05, 12:23 PM
  #7  
DVD Talk Reviewer/ Admin
 
Adam Tyner's Avatar
 
Join Date: Sep 1999
Location: Greenville, South Cackalack
Posts: 21,860
Received 8 Likes on 5 Posts
Originally Posted by moorehed
CRM, I think I am the resident PHP guy. perhaps there's more. Anyways, this isn't really a PHP problem but an HTML one.
There are quite a few PHP guys here. I've been doing it for professionally for more than five years now.

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

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


Thread Tools
Search this Thread

Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service - Do Not Sell My Personal Information

Copyright 2018 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.