Passing arrays via checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
283Banfil
Forum Newbie
Posts: 7
Joined: Mon Sep 14, 2009 10:38 am

Passing arrays via checkboxes

Post 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();
}
?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Passing arrays via checkboxes

Post 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[]".
283Banfil
Forum Newbie
Posts: 7
Joined: Mon Sep 14, 2009 10:38 am

Re: Passing arrays via checkboxes

Post by 283Banfil »

It still only passes with the last choice?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Passing arrays via checkboxes

Post 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.
283Banfil
Forum Newbie
Posts: 7
Joined: Mon Sep 14, 2009 10:38 am

Re: Passing arrays via checkboxes

Post 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.
283Banfil
Forum Newbie
Posts: 7
Joined: Mon Sep 14, 2009 10:38 am

Re: Passing arrays via checkboxes

Post 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();
}
?>
Post Reply