Jump to content

checkbox group in php form


ilikephp

Recommended Posts

<input type="checkbox" name="father" value="answer 1" />
answer 1</label>
                    <br />
                    <label>
<input type="checkbox" name="father" value="answer 2" />
                      answer 2</label>
                    

 

 

Link to comment
Share on other sites

I tried to put this in my code:

 

in the insert to database:

GetSQLValueString($_POST['father'][1], $_POST['father'][2]),

 

In the form:

<label>
<input type="checkbox" name="father[]" value="answer 1" /> answer 1</label>  <br /><label>
<input type="checkbox" name="father[]" value="answer 2" /> answer 2 </label> <br />

 

 

I got an error :S

Link to comment
Share on other sites

The code in your first post:

GetSQLValueString($_POST['father'], "text"),

indicates this function expects a value and a type.

 

However, in your next post:

GetSQLValueString($_POST['father'][1], $_POST['father'][2]),

you are passing two values.

 

What exactly are you trying to do with the result of the checkbox array? Are they supposed to go into a single field, or are there separate fields for each possible answer?

 

Be aware that you may get any number of elements in the "father" array. And, in fact, the "father" element will NOT exist in the array if the user does not check any boxes. For example:

 

<input type="checkbox" name="father[]" value="answer 1" /> answer 1</label>  <br /><label>
<input type="checkbox" name="father[]" value="answer 2" /> answer 2 </label> <br /><label>
<input type="checkbox" name="father[]" value="answer 3" /> answer 3 </label> <br />

 

If the user checks the first and third box, the resulting array will be:

$_POST[father][0] = 'answer 1';
$_POST[father][1] = 'answer 3';

 

If you are trying to put all the values in a single field, then perhaps something like this will work:

$fatherValue = (isset($_POST['father']) ? implode(',', $_POST['father']) : '');

GetSQLValueString($fatherValue, "text"),

 

If you want them in separate fields; or even better, in separate rows of a child table, you will have to loop through each element and build and insert statement.

Link to comment
Share on other sites

We would have to know the structure of the database, specifically the table you are trying to put them into. The code snippets you provided do not indicate to structure of your database and tables. If you are trying to put them in a single field as a comma-separated string, then, yes, you would use the implode() function to build that string.

 

You could give it a try and see if it does what you want.

Link to comment
Share on other sites

I want them to be displayed in the same field with a comma to separate them.

 

I put this code in the form:

<input type="checkbox" name="father[]" value="answer 1" /> answer 1</label>  <br /><label>
<input type="checkbox" name="father[]" value="answer 2" /> answer 2 </label>  <br />

			<?php  $fatherValue = (isset($_POST['father']) ? implode(',', $_POST['father']) : '');  ?>

 

and in the $insertSQL = sprintf("INSERT INTO ....

GetSQLValueString($fatherValue, "text"),

 

 

I dont know if I did something wrong but I am getting Null value in the Mysql database

Link to comment
Share on other sites

Echo your SQL just before you execute it. Take a close look at it to see if there is something wrong.

 

Test the return value from the execution (i.e. mysql_query() or whatever) to see if it succeeded. -- If you're using mysql, you can echo mysql_error() to see the error message.

 

Turn error reporting on in your PHP script to see if there are any PHP errors causing problems.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.