Forum Moderators: coopster
I've searched for around two hours now trying to find a fix for my specific problem, but I've found nothing that's worked. I have a very simple form, so I'm hoping it's a quick fix.
I have an HTML form that has three checkboxes with the same name. I need the user to be able to check none or all three and then pass the values of any that are checked. Here's what it looks like in the form:
<input name="bodyfontdecoration[]" type="checkbox" id="bodyfontdecoration[]" value="underline " />
<input name="bodyfontdecoration[]" type="checkbox" id="bodyfontdecoration[]" value="line-through " />
<input name="bodyfontdecoration[]" type="checkbox" id="bodyfontdecoration[]" value="overline" />
Now, once the form is POSTed, I tried calling it up on the PHP page with just a simple:
$body_fontdecoration = $_POST['bodyfontdecoration'];
and then echo the $body_fontdecoration. But, as I've found out from my hours of searching, PHP will only read the last checkbox that is checked. So, if my user checks all three boxes, how do I get it to pass the exact values that are in the inputs? If all three are checked, I need it to pass as "underline line-through overline". That's my biggest question.
And lastly, if none of the boxes are checked, can I just add this to check:
if (_POST['bodyfontdecoration']!=NULL) {
//use the code to print out all the checkbox values here
}
Would that work? Thanks so much for helping me wade through this PHP stuff!
Excepted that id is not required in the <input> element, only the name is necessary.
For multiple checkboxes, see one of my previous post that has a short explanation on how to deal with array and multiple checkboxes : [webmasterworld.com...]
My 2cents
In my form, the checkbox values are "underline ", "line-through ", and "overline". If the user checks all three of those checkboxes, I need those values to get to the PHP page, so the user can allow their text to be underlined, overlined, etc. Then, if they checked all three checkboxes, I need the PHP page to print exactly this:
text-decoration: underline line-through underline;
Right now, I have other fields setup just fine. For instance, I have a radio button to define the font family that when selected, has this code on the PHP page:
if $radio_button!=NULL {
$radio_print="font-family: " . _POST['radio_button'] . "; ";
And all is well. I need to do this exact same thing, but I need the PHP page to print out all three values of the checkboxes in a row with nothing separating them. Seems like it should be so easy, but I'm so stumped! Thanks so much for y'alls help.
[edited by: JohnDubya at 6:15 am (utc) on Nov. 1, 2006]
A.) Get rid of your "ID" tags. If you're using DW or FP adjust your setting so that these are no longer included -- they cause problems with php... trust me.
B.) To see if the checkbox has no value you do not want to use NULL. Instead, you want a more concrete assesment. I would suggest placing a user default value in the checkbox like 'None'.
C.) Accept you checkbox values as an array (as you're forcing them to be with "[]") or by a non-value (implied) by doing:
if (isset($_POST['bodyfontdecoration']) && $_POST['bodyfontdecoration']!= 'none'){
// Keep Going
}
D.) Now that you know you have a workale value, you need to figure out if you have an array and which values you have so you can assign output behaviors like (building on before):
if (isset($_POST['bodyfontdecoration']) && $_POST['bodyfontdecoration']!= 'none'){if (is_array($_POST['bodyfontdecoration'])){
$textDecoration = implode(" ", $_POST['bodyfontdecoration']); // format your array for use
} else {$textDecoration = $_POST['bodyfontdecoration']; // no array -> print single value
}
}
Now, all you should need to do is echo "$textDecoration" in your script when you send it and the format will be present.