Simple Qustion

Hey all! Long time since I’ve needed some help, but I’m coding something and cannot remember how to do it right, but know I’m close.

I have a form which has multiple checkboxes. What I want to do, is have a loop take the input from each checkbox and preform a MySQL query with it. For example, say the checkboxes are for emails. You can select multilple emails, choose delete, and have a loop preform the query. Here’s my code, the html part being the part I’m sketchy on.

<input type="checkbox" name="variable[1]">
<input type="checkbox" name="variable[2]">
<input type="checkbox" name="variable[3]">
etc

[code]foreach($variable as $id){

Mysql Query
}[/code]

As far as I remember, that should turn $variable into an array, which I can then use a foreach to preform a query.

Where am I going wrong?

while(list($key,$value) = each($variable))
{
//run query here
}

That should get the job done.

I am almost 100 percent sure that the HTML part will NOT give you an array. I tried it on my system and it didn’t pass ANYTHING. (from the checkboxes). I renamed ONE of them (the last one) to just variable and it came across fine.

I think if you want to have an ARRAY you need to name your variables on the input page as follows

<input type="checkbox" name="variable1">
<input type="checkbox" name="variable2">
<input type="checkbox" name="variable3">
etc

And then in your parsing code you can assign it to an array as such:

<?
// the 10 is assuming that there are 10 instances of variableX. 
//it should be adjusted appropriately.
for ($i=1; $i<10; $i++){
        $variable = "variable".$i;
        $array[$i] = $$variable;
}

This will put your data into an array for PHP processing.

I am not even sure if HTML can submit an array. Not that I have ever researched it or tried. I just have always done it in a manner as described above.

You could also do something like

<input type="checkbox" name="del_id[]" value="<?echo $id;?>">
<input type="checkbox" name="del_id[]" value="<?echo $id;?>">
while(list($key,$value) = each($variable)) 
{ 
//run query here 
} 

I know this works. Use it all the time for deleteing multiple items.

Sponsor our Newsletter | Privacy Policy | Terms of Service