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
Php and Checkboxes
Moderator: General Moderators
Would something like
work?
(Posted as code and not PHP because the combination of escaped quotes and quoted array indices seems to completely confuse the highlighter)
Code: Select all
$checkboxes=array(array("Name"=>"check","Value"=>"me"),
...);
foreach ($checkboxes as $aBox)
{
echo "<input type="checkbox" name="{$aBoxї"Name"]}" value="{$aBoxї"Value"]}";
echo ($$aBoxї"Name"]==$aBoxї"Value"] ? " selected="selected"" : "");
echo " />\n";
}(Posted as code and not PHP because the combination of escaped quotes and quoted array indices seems to completely confuse the highlighter)
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:
When posted the value of $check will be "me" when selected -- otherwise... it won't be "me".
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>That's what I was doing with my code snippet above, using the ternary operator '?:' on the second echo line.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?
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";Variable variables:
Notice in the second echo line:
Code: Select all
echo ($$aBox["Name"]==$aBox["Value"] ? " selected="selected"" : "");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.