Page 1 of 1

Php and Checkboxes

Posted: Fri Jul 25, 2003 10:24 am
by aphelion
Hey there,
I've got a problem here that I can't seem to work out. There is a rather large form with a lot of checkboxes, and I need to check the html query line and check the boxes accordingly. Rather than do this:
<input type="checkbox" name="check" value="me <? if ($check==me) { echo "SELECTED"; } ?>> for each checkbox

Are there built-in variables in php that would allow me to say:
<input type="checkbox" name="check" value="me <? if ($name==$value) { echo "SELECTED"; } ?>>

So that $name for this particular case would equal check and $value would equal me. Do such variables exsist? This method would allow me to cut and paste the same piece of code over and over without having to manually type everything in each time. If no such variables exsist, then does anyone have any other ideas?

Thanks,
Jesse Vernon

Posted: Fri Jul 25, 2003 10:44 am
by nielsene
Would something like

Code: Select all

$checkboxes=array(array("Name"=>"check","Value"=>"me"),
                              ...);
foreach ($checkboxes as $aBox)
&#123;
   echo "<input type="checkbox" name="&#123;$aBox&#1111;"Name"]&#125;" value="&#123;$aBox&#1111;"Value"]&#125;";
   echo ($$aBox&#1111;"Name"]==$aBox&#1111;"Value"] ? " selected="selected"" : "");
   echo " />\n";
&#125;
work?

(Posted as code and not PHP because the combination of escaped quotes and quoted array indices seems to completely confuse the highlighter)

Posted: Sat Jul 26, 2003 1:50 pm
by aphelion
Hmm, that would, but is there anyway I can just extract the name and value variables out of the html tags and get them into some sort of if/then statement?

Posted: Sun Jul 27, 2003 4:49 pm
by Slippy
I'm not sure if I understand what you mean by extract the name value variables?

When you hit post your form to your .php script the name value pairs will get submitted directly to the script.

That is -- if you have a check box in this form:

Code: Select all

<input type="checkbox" name="check" value="me" SELECTED>
When posted the value of $check will be "me" when selected -- otherwise... it won't be "me".

Posted: Mon Jul 28, 2003 11:31 pm
by nielsene
aphelion wrote:Hmm, that would, but is there anyway I can just extract the name and value variables out of the html tags and get them into some sort of if/then statement?
That's what I was doing with my code snippet above, using the ternary operator '?:' on the second echo line.

You would simple stick all the name/value pairs into the $checkboxes array (one sample line shown at the top of my script before the "...". And then the loops handles printing out all the html for all the checkboxes includeing setting those required. IE you only need the loop once, even if printing out 100 checkboxes, so its simpler than a system than needs an if for each checkbox.

The second echo is really the heart of it using a few slightly "advanced" concepts -- the ternary operator and variable variables.

If you aren't familiar with these here's how the work:

The ternary operator "?:"
$something = ($x==$y ? "trueval" : "falsevar");
The expression before the '?' in the example $x==$y is first evaluated. If its true, then $something is set to what's after the '?' (in the example "trueval") otherwise it uses the value after the ":". Its almost like

Code: Select all

if ($x==$y)
   $something="trueval";
else
  $something="falseval";
The ternary operate is extremely useful but you need to be careful as it does tend to produce less readable code.

Variable variables:
Notice in the second echo line:

Code: Select all

echo ($$aBox["Name"]==$aBox["Value"] ? " selected="selected"" : "");
There there is a double '$' on the first variable. What this does:
First PHP looks up the value of $aBox["Name"] in my example this equals check (like your needed code). Then the second dollar sign causes it to look up the value of the variable $check and compare it to the value of the checkbox. If $check was set by this form before, then it will be equal to the value and the ternary operator will cause the selected=selected string to be added.

I think the code is doing exactly what you asked.