Link to home
Start Free TrialLog in
Avatar of dnatal
dnatal

asked on

PHP checklist form display results

I have a checklist of names that come from a database that I display on a page. The user then checks off the ones that they want to send email messages to and submits the form. The form has them constructed as:

<form method="GET" action="mail_out_stories.php">
...
//loop through all the user_id values...
...
<input type="checkbox" name="var'.$record['user_id'].'" value="'.$record['user_id'].'">

So, the onces checked by the user get passed as var22=22, var35=35, etc. for each one selected.

Then, in the php file called I was using:

...
for ($i = 1; $i < 1000; $i++) {
      $varname = "var".$i;
      if (isset ($$varname)) {
//do what I need to for each of these that were set...

This worked fine in PHP 4 but isn't working in PHP5. In any case, I know it isn't really the way to accomplish my goal (of seeing what the user has checked) as it isn't very efficient, and won't work once the index number exceeds my maximum loop count (as well as being, in general, sloppy programming!)

I figure I need to use an array to pass it, and don't want to use a GET, I want to use a POST, but I'm not sure how to do this.
ASKER CERTIFIED SOLUTION
Avatar of snoyes_jw
snoyes_jw
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maUru
maUru

you can use:

<input type="checkbox" name="var[]" value="1">
<input type="checkbox" name="var[]" value="2">
<input type="checkbox" name="var[]" value="3">

and then check them (with post) using this:

<form method="POST">

and the php code to read:

foreach ($_POST['var'] as $val) {
  // do stuff to $val
}
Avatar of dnatal

ASKER

Thanks! I figured it was easier than I was making it...
didnt you also ask for a way to use arrays, and about ineffeficient coding and it wont work once it passes 1000....
Avatar of dnatal

ASKER

Yeah, I did. I probably should have increased, then split, the points, but snoyes_jw's answer came first and addressed the issues of POSTing and using an array and so was a full answer...your answer was right, too, just it came after snoyes_jw's. Thanks for your help, if I could go back and award you points I would (I wasn't sure of the protocol...)