Forum Moderators: coopster

Message Too Old, No Replies

Pass multiple checkboxes from HTML form to PHP

         

JohnDubya

5:08 am on Nov 1, 2006 (gmt 0)

10+ Year Member



I'm about to pull my hair out and run around screaming random nothings. Not that that would be out of the ordinary or anything.

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!

tomda

5:46 am on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nothing wrong with your script...

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

justgowithit

5:56 am on Nov 1, 2006 (gmt 0)

10+ Year Member



JohnDubya,

I'll post you some code that will help you on your way but first I need to know what you're trying to accomplish with the $_POST values. Will these be used to manipulate text?

JohnDubya

6:11 am on Nov 1, 2006 (gmt 0)

10+ Year Member



What I'm basically doing is allowing people to design the look of their site with some basic CSS entries. They just click buttons on an HTML form, and depending on what choices they make, a PHP page prints out the CSS code.

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]

justgowithit

6:25 am on Nov 1, 2006 (gmt 0)

10+ Year Member



You haven't responded and I'm going to bed so I'll make this short.

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.

JohnDubya

10:00 pm on Nov 1, 2006 (gmt 0)

10+ Year Member



Wow, thank you so much! Your code worked perfectly. It's really strange. I used implode and isset and all these things, and it never worked for me. Somehow, you must have just had everything perfect, and I was probably missing something since all I know about PHP is what I've picked up from reading other people's code. Again, thank you so much for helping me find the solution!