bluegray Posted May 31, 2010 Share Posted May 31, 2010 Hi Php Freaks! I'm trying to submit checkboxes through a form, but it doesn't seem to be working like "text" type inputs. Here's the Xhtml: <label for="veggies">Vegetables <input type="checkbox" name="testbox [veg]" id="veggies" value="veggies" /> </label> Here's the Php: $testbox = $_POST ['testbox'] ; var_dump ($testbox) ; Here are my results: Notice: Undefined index: testbox in C:\Program Files\WAMP\www\test\form.php on line 72 NULL And here is me getting rocked by check boxes: :'( Anywhere I can go to be explained the nuances of checkboxes? Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 31, 2010 Share Posted May 31, 2010 The name you gave your's contains a space and it is an array. It produces the following (php converts the space in the name to an under-score in order to make it a valid variable name) - [testbox_] => Array ( [veg] => veggies ) Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1065835 Share on other sites More sharing options...
bluegray Posted June 1, 2010 Author Share Posted June 1, 2010 The name you gave your's contains a space and it is an array. It produces the following (php converts the space in the name to an under-score in order to make it a valid variable name) - [testbox_] => Array ( [veg] => veggies ) I'm not sure I get what you mean...I don't see a space in the name of my array. Why would php randomly insert an underscore? Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066196 Share on other sites More sharing options...
premiso Posted June 1, 2010 Share Posted June 1, 2010 name="testbox [veg]" Notice the space before [veg]. That is why the testbox has a space in it. name="testbox[veg]" Will solve that issue. Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066197 Share on other sites More sharing options...
bluegray Posted June 1, 2010 Author Share Posted June 1, 2010 name="testbox [veg]" Notice the space before [veg]. That is why the testbox has a space in it. name="testbox[veg]" Will solve that issue. Ah, so whenever referring to an array, am I supposed to leave no spaces between the array name and the index/key box? Edit: Rats! I removed the the space, but am still getting the undefined index notice once I submit the form. =( Just as an experiment, I've switched the input "type" to text, and the problem went away. So it's definitely an issue with the kind of input. Does anyone know how to circumvent this problem? Other than avoiding checkboxes altogether. Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066242 Share on other sites More sharing options...
premiso Posted June 1, 2010 Share Posted June 1, 2010 You do not need to do spaces after, IE: $array ['index'] Is generally looked down upon. $array['index'] Is the "preferred" way by most coders. Not sure where you got the idea to space between them. But show the full relevant code, specifically where the notice error is thrown, so we can help you. Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066246 Share on other sites More sharing options...
bluegray Posted June 1, 2010 Author Share Posted June 1, 2010 You do not need to do spaces after, IE: $array ['index'] Is generally looked down upon. $array['index'] Is the "preferred" way by most coders. Not sure where you got the idea to space between them. But show the full relevant code, specifically where the notice error is thrown, so we can help you. Here's the portion of the form that's bringing up the problem: <!-- Check Boxes! --> <div class="form_sect"> <span class="form_cat">Favorite Foods</span> <label for="fruits">Fruits <input type="checkbox" name="testbox[fruits]" id="fruits" value="fruits" /> </label> <label for="salad">Salad <input type="checkbox" name="testbox[salad]" id="salad" value="salad" /> </label> <label for="veggies">Vegetables <input type="checkbox" name="testbox[veg]" id="veggies" value="veggies" /> </label> <label for="soup">Soup <input type="checkbox" name="testbox[soup]" id="soup" value="soup" /> </label> </div> Here's the line that's being pointed to in the notice: $testbox = $_POST['testbox'] ; And that's really all that's relevant as far as I can tell. This is just some added context: "Just as an experiment, I've switched the input "type" to text, and the problem went away. So it's definitely an issue with the kind of input. Does anyone know how to circumvent this problem? Other than avoiding checkboxes altogether." Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066257 Share on other sites More sharing options...
premiso Posted June 1, 2010 Share Posted June 1, 2010 Well with checkboxes, if the item is not checked it is not going to be in the POST data. Is one of the checkboxes checked? To avoid the notice error you can do this: $testbox = isset($_POST['testbox'])?$_POST['testbox']:null; Which will set textbox to null if no items were checked. Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066289 Share on other sites More sharing options...
bluegray Posted June 2, 2010 Author Share Posted June 2, 2010 Well with checkboxes, if the item is not checked it is not going to be in the POST data. Is one of the checkboxes checked? To avoid the notice error you can do this: $testbox = isset($_POST['testbox'])?$_POST['testbox']:null; Which will set textbox to null if no items were checked. Can you explain the "?" and the ":" please? I'm not familiar with those. Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066723 Share on other sites More sharing options...
premiso Posted June 2, 2010 Share Posted June 2, 2010 Ternary operator, it acts a shortened if else. Basically it goes if (isset($_POST['testbox'])) { $testbox = $_POST['testbox']; }else { $testbox = null; } Same thing, just shortened. Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066754 Share on other sites More sharing options...
bluegray Posted June 2, 2010 Author Share Posted June 2, 2010 Ternary operator, it acts a shortened if else. Basically it goes if (isset($_POST['testbox'])) { $testbox = $_POST['testbox']; }else { $testbox = null; } Same thing, just shortened. Sweet stuff. Okay... I used the shorter version because as you said, it's neater. I'm still having some issues with the nature of checks though. How would you handle processing them into an SQL database when they're part of a larger form? The problem is that when, say 5 out of 10 boxes are checked, the array that's filled only has 5 elements; whereas if it was a text type input the array would have 10 elements with 5 nulls. Right now, it seems the best plan is to sort them into a separate array from text type inputs, with an argument representing the number of values that Could be received, and have a custom function fill all the empties with a null. Quote Link to comment https://forums.phpfreaks.com/topic/203449-why-are-checkboxes-such-a-pain/#findComment-1066792 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.