Welcome to WebmasterWorld Guest from 50.16.52.237

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP: Handling multiple form inputs with same name

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

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 0


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

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

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


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.

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

New User

10+ Year Member

joined:Oct 29, 2002
posts:28
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


The PHP manual is a rather good resource to learn about PHP. Its PHPs manual after all.

[php.net...]

Andreas

empires

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

Inactive Member
Account Expired

 
 


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.

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

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


Welcome to WebmasterWorld empires

get2jean

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

Inactive Member
Account Expired

 
 


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?

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members