Forum Moderators: coopster

Message Too Old, No Replies

php checkboxes - 2 page form

         

tn_2011

3:05 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



Hi

I have 1 form that I need to split into two. Currently all the data gets stored into a database.

I pass the data entered from page 1 to page 2 through the use of hidden fields. I have having trouble carrying across multiple checkbox values.

I'll start by explaining the form. It is an order samples form. So 1 person can order multiple samples. The samples ordered goes into a database on a new row as my client monitors how many samples are ordered.

Here is the layout of the checkboxes:
<input name="sample[]" type="checkbox" value="Sample1">
<input name="sample[]" type="checkbox" value="Sample2">
<input name="sample[]" type="checkbox" value="Sample3">
<input name="sample[]" type="checkbox" value="Sample4">

and heres my code to insert into the database
foreach ($_POST['sample'] as $value) {
$foo = mysql_query("Insert into table SET sample ='$value', othervalues.........);

The checkboxes will remain on page 1 and the other fields will go to page 2...

How can I carry the checkbox value to page two... then process the form and insert into the database?

Hope this makes sense...

hope someone can help

Thanks

tn_2011

10:32 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



Any ideas?

rocknbil

4:52 pm on Dec 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Similar discussion [webmasterworld.com]?

You step through the array of boxes that are checked and output hidden fields - or - store them in a comma separated list. The second might work better because for the first each field name will have to be unique. You could try to do an array of hiddens the same way you do checkbox forms (<input type="hidden" name="name[ ]"...) (Never tried that . . . )

Alternatively you can skip carrying the hidden fields and use session variables, but your idea is more stable in that it doesn't rely on cookies to maintain a connection with the user's browser.

tn_2011

11:24 am on Dec 22, 2011 (gmt 0)

10+ Year Member



Thanks rocknbil...

How can I store them in multiple hidden fields?

At the moment I store the checked values in 1 hidden field (separated by comma) ..

the values are stored in a hidden field using this code:
<?php echo implode(",", $_POST['sample']) ?>

How can I then store the values each on a new row in the database? I've tried various ways (i.e. as shown in my 1st post) but no data is inserted

any ideas?

rocknbil

5:18 pm on Dec 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, so you have comma separated the values, which should give you

<input type="hidden" name="some-name" value="once, doce, trese, catorce">

So all you need to do is re-explode them on "receipt" into an array again.

$vals = explode(',',$_POST['some-name']);

foreach ($vals as $val) {
$query = "insert into table (fieldname) values('$val')";
mysql_query($query) or die("Cannot insert $val: " . mysql_error());
}