Page 1 of 1

Passing arrays via checkboxes

Posted: Mon Sep 14, 2009 10:40 am
by 283Banfil
I'm stumped. I've been at this for days!! Huh
I'm trying to pass an array via checkboxes, using php and mysql. I have the following 2 pages, can someone please tell me what I've got wrong. I can pass the array to the second page, and have it echo(print) correctly, but when it gets to the database, it will only give me the last choice. I also can get it to post "array" to the database, but I don't want that either!!

Insert form:

Code: Select all

<?php 
 
echo "<form method=post action='form3.php'>";
echo "<table border='0' cellspacing='0' style='border-collapse: collapse' width='100' >
<tr bgcolor='#ffffff'>
<td width='25%'><input type=checkbox name=providers[] value='BlueCross BlueShield'></td>
<td width='25%'>&nbsp;BlueCross BlueShield</td>
<td width='25%'><input type=checkbox name=providers[] value='HealthPartners'></td>
<td width='25%'>&nbsp;HealthPartners</td>
<td width='25%'><input type=checkbox name=providers[] value='PreferredOne'></td>
<td width='25%'>&nbsp;PreferredOne</td>
</tr>
 
<tr><td colspan =6 align=center><input type=submit value=Select></form></td></tr>
</table>"; ?>
Results Action page

Code: Select all

<?php 
$providers=$_POST['providers'];
while (list ($key,$val) = @each ($providers)) {
echo "$val,\n";
$mysql="INSERT INTO oe.hsa SET
    providers='$val'";} 
if (@mysql_query($mysql)) {
echo 'New Registration added';
} else {
echo mysql_error();
}
?>

Re: Passing arrays via checkboxes

Posted: Mon Sep 14, 2009 11:50 am
by Ollie Saunders
You must specify names so that they are unique. E.g.: -
name="providers[0]"
name="providers[1]"
name="providers[2]"

not:
name="providers[]"
name="providers[]"
name="providers[]".

Re: Passing arrays via checkboxes

Posted: Mon Sep 14, 2009 12:01 pm
by 283Banfil
It still only passes with the last choice?

Re: Passing arrays via checkboxes

Posted: Mon Sep 14, 2009 12:19 pm
by Ollie Saunders
Remove the @s from your code. Suppressing errors will make life harder for you.

Here you're constructing a query that inserts a single value:

Code: Select all

$mysql="INSERT INTO oe.hsa SET providers='$val'";
Each iteration of the while loop you assign a different query to $mysql for a different single value, equally, in each iteration, you discard that query; do nothing with it; and allow the next iteration to replace it with another assignment to $mysql.

The simplest fix might be to change "$mysql = " to "$mysql[] =" and go from there.
You can use var_dump($mysql) before you pass it to mysql_query() if you want to see what it contains.

Re: Passing arrays via checkboxes

Posted: Mon Sep 14, 2009 1:46 pm
by 283Banfil
Hmm. Still can't get it to pass array to database. I get the desired results when I use the _dump, but when I change it to mysql_query i just get the last one.

Re: Passing arrays via checkboxes

Posted: Mon Sep 14, 2009 2:31 pm
by 283Banfil
I got it to work. It took me 2 days, but I did it.

Code: Select all

<?php 
foreach($_POST['providers'] as $val) {
$providers .= "$val\n";
$mysql="INSERT INTO oe.hsaTest SET
    providers='$providers'";}   
if (mysql_query($mysql)) {
echo 'New Registration added';
} else {
echo mysql_error();
}
?>