homepage Welcome to WebmasterWorld Guest from 54.204.141.129
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP: Handling multiple form inputs with same name
Allen




msg:1310295
 4:50 pm on Feb 7, 2003 (gmt 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

 

jatar_k




msg:1310296
 7:12 pm on Feb 7, 2003 (gmt 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.

aaronc




msg:1310297
 12:39 am 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>

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




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

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

[php.net...]

Andreas

empires




msg:1310299
 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




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

Welcome to WebmasterWorld empires

get2jean




msg:1310301
 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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved