Jump to content

checkbox + database error


bamfon

Recommended Posts

well i got a few checbox (look below for code)

i want to make it so that if i click the checkbox's it sends all the check box value to one column with "," in between them

      
<input type="checkbox" name="genre" value="fighting" id="genre"/>fighting
<input type="checkbox" name="genre" value="school life" id="genre" />school life
<input name="genre" type="checkbox" id="genre" value="king" id="genre" />king 
<input name="genre" type="checkbox" id="genre" value="him" />him

Link to comment
https://forums.phpfreaks.com/topic/224913-checkbox-database-error/
Share on other sites

You need to provide your php, as this is what will put the information into the database.

 

You will also need to set the values of the checkboxes into an array. Something that I found out this week myself.

 

When you process the form, you can check through the checkboxes array to see if it's set or not, and then append the value to a checkboxes string, which is then put into the datase.

 

Denno

So basically, you want the values from all the boxes that are checked? If so, the first problem is that you'll need to change the checkbox code.

 

Your current code has four checkboxes with the same name. So when the data is sent back to the server you'll only have one value if more than one is checked.

 

<input type="checkbox" name="genre" id="genre" value="fighting" />fighting
<input type="checkbox" name="genre" id="genre" value="school life" />school life
<input type="checkbox" name="genre" id="genre" value="king" />king
<input type="checkbox" name="genre" id="genre" value="him" />him

 

 

Instead you'll want the checkboxes to have different names and ids.

 

<input type="checkbox" name="genre1" id="genre1" value="fighting" />fighting
<input type="checkbox" name="genre2" id="genre2" value="school life" />school life
<input type="checkbox" name="genre3" id="genre3" value="king" />king
<input type="checkbox" name="genre4" id="genre4" value="him" />him

 

 

Now you'll have four different variables to work with in your script:

 

echo $_GET['genre1'] . ', ';
echo $_GET['genre2'] . ', ';
echo $_GET['genre3'] . ', ';
echo $_GET['genre4'] . ', ';

 

 

Note that if your form is set to method="post" you'll need to change the above variables to $_POST['genre1'], etc.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.