Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP checklist form display results

Posted on 2006-07-10
5
Medium Priority
?
2,751 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:dnatal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 1000 total points
ID: 17076566
I suspect the only difference is that for PHP5, you have register_globals turned off.  So change
if (isset($$varname))
to
if (isset($_GET[$$varname]))

To use an array instead, name your checkboxes as var[22], var[35], etc.  They will automagically be an array in PHP, and you can do
foreach ($_GET['var'] as $user_id=>$value) {
  // do what you need with $user_id and $value.

To use POST instead of GET, just change the form method and the super global (the $_GET in the revision above) to POST.
0
 
LVL 7

Expert Comment

by:maUru
ID: 17076569
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
}
0
 
LVL 2

Author Comment

by:dnatal
ID: 17077469
Thanks! I figured it was easier than I was making it...
0
 
LVL 7

Expert Comment

by:maUru
ID: 17077487
didnt you also ask for a way to use arrays, and about ineffeficient coding and it wont work once it passes 1000....
0
 
LVL 2

Author Comment

by:dnatal
ID: 17077618
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...)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question