Hello,

I am currently making a form that includes a "calendar" made up of checkboxes. The customer checks the boxes he/she would like. All the check boxes are given the same name so that javascript will put them into an array and i can go through and calculate a deposit based on how many days are checked. This works fine. The problem comes when i try to pass the information to a php file to insert the data into my database. When i refernce the check boxes (named Day) my php page only reads the first checkbox. Is there a way to pass an array of checkboxes to a php file? here's the code:

html form:

index.php:
<tr align="center">
<td>6/23 - 6/27</td>
<td><input name="Day" type="checkbox" checked></td>
<td><input name="Day" type="checkbox"></td>
<td><input name="Day" type="checkbox" checked></td>
<td><input name="Day" type="checkbox"></td>
<td><input name="Day" type="checkbox" checked></td>
</tr>

form_process.php
$sqlquery = "INSERT INTO campers
(
last_name,first_name,address,
city, state, zip,
gender,
june16, june17
)

VALUES
(
'$camp_lname','$camp_fname','$address',
'$city', '$state', '$zip',
'$gender',
'$Day[0]', '$Day[1]'
)";

$results = mysql_query($sqlquery);

All the other data in my querey ($camp_lname, $camp_fname, etc) is entered correctly into the database but the $Day is just given the value of "on" and so the $Day[0] is therefore given the value of 'o' and the $Day[1] is given the value of 'n'. I am currently trying to use hidden fields in my form to pass the values to the processing page but I am still facing roadblocks. Any advice will be apprechiated.

Thank you,
Robin

    After posting your form you can check the post variables that were actually posted with print_r($_POST);. You will notice that there is no such thing as an array of checkboxes with the same name because you have to define only one variable(input element, select, textarea,....) per form. In your case you receive an array such Array ( [Day] => on ) because there is only variable: 'Day'. You must define elements with unique names to make the thing work.

      Thank you. The problem i was having is that i need all the checkboxes to have the same name becuase i can then reference all the checkboxes as an array in my javascript function before the form is submited. I figured out how to do what i need using hidden text fields. thanks again!

        mika79 wrote:

        You will notice that there is no such thing as an array of checkboxes

        Not true at all - you can have multiple elements submit under one variable as an array - just add '[]' onto the end of the name.

        zoey: Either way, don't forget to mark this thread resolved.

          Write a Reply...