Welcome to WebmasterWorld Guest from 54.226.246.160

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP: Handling multiple form inputs with same name

     

Allen

4:50 pm on Feb 7, 2003 (gmt 0)

10+ Year Member



Hi,

I know this can be done. At the moment, on my scripts, when listing table entries, I have a radio button on each called 'id', which, obviously, I can access as $_POST['id'].

What I want to do:
Basically, I would like to change the radio button to a checkbox so that I can delete any number of items at a time (jsut by checking all the ones I want to delete).

How would these be accessed in a PHP script? Would an array be created, eg: $_POST['id']['0'] $_POST['id']['1']....?

TIA
Allen

jatar_k

7:12 pm on Feb 7, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I think if they have the same name they will just try to pass multiple values into the same var. It is probably easier to give them different names id1, id2 id3, etc.

I don't know how you pass the id but here is some guessing.

if you have 10 checkboxes like this with hidden id paired to it as hidden vars

<input type="checkbox" name="del1" value="yes">
<input type="hidden" name="id1" value="<?= $someid?>">

you could do something like
$counter = 1;
while ($counter < 11) {
$dodel = "del" . $counter;
if ($_POST[$dodel] == "yes") {
$delid = "id" . $counter;
$delquery = "delete from table where id=" . $_POST[$delid];
mysql_query($delquery);
}
$counter++;
}

I used a set number but you could also pass the count as a hidden form var and do $counter < $count or something. I also used extra steps in hereto make it alittle easier.

aaronc

12:39 am on Feb 8, 2003 (gmt 0)

10+ Year Member



This is how you do it.
<input type=checkbox name=id[] value=123>
<input type=checkbox name=id[] value=456>

When you submit the form, the id variable will be an array with whatever values are checked.
If both checkboes are checked, the array will have 2 values in there 123 & 456

andreasfriedrich

1:26 am on Feb 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The PHP manual is a rather good resource to learn about PHP. Itīs PHPīs manual after all.

[php.net...]

Andreas

empires

8:00 pm on Feb 8, 2003 (gmt 0)



This is how you do it.
<input type=checkbox name=id[] value=123>
<input type=checkbox name=id[] value=456>

That's the method.

Also remember in the ACTION script processing the
form entries to first check that

isset($_POST['id'])

because (unlike a radio button) you may have
situations where NONE of the checkboxes are
selected, and thus $id array won't exist and
would be undefined when referenced in the
form processing script.

jatar_k

8:09 pm on Feb 8, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome to WebmasterWorld empires

get2jean

4:09 pm on Feb 13, 2003 (gmt 0)



how do you do the same thing as: -

"if you have 10 checkboxes like this with hidden id paired to it as hidden vars

<input type="checkbox" name="del1" value="yes">
<input type="hidden" name="id1" value="<?= $someid?>">

you could do something like
$counter = 1;
while ($counter < 11) {
$dodel = "del" . $counter;
if ($_POST[$dodel] == "yes") {
$delid = "id" . $counter;
$delquery = "delete from table where id=" . $_POST[$delid];
mysql_query($delquery);
}
$counter++;
} "

if the number of checkboxes can change each time?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month