tgavin Posted October 6, 2012 Share Posted October 6, 2012 I'm working with a checkbox group called food[]. One of the checkboxes has the value of 'steak'. this is the one i'm testing with. I'm using a function to build and print the checkbox. The function accepts an array ($data) of names and values for each checkbox. The name of the checkbox is contained in $data['name'], which is passed through the function. if I var_dump($data['name']) from within the function it prints 'food[]'. perfect. I'm now trying to get the POST values. if i do this, i get an undefined index error if(isset($_POST[$data['name']])) { var_dump($_POST[$data['name']]); } if i do this, it prints 'steak' if(isset($_POST['food'])) { var_dump($_POST['food']); } what am i doing wrong? Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 var_dump($data); What's in it? The undefined index means that $data['name'] cannot be set to 'food'. It's set to something that doesn't exist in $_POST. You can also do a print_r() on any of those arrays to see whats in them. Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Author Share Posted October 6, 2012 (edited) var_dump($data); array(6) { ["type"]=> string( 8 ) "checkbox" ["name"]=> string(6) "food[]" ["value"]=> string(5) "steak" ["string"]=> string(10) "id="steak"" ["checked"]=> bool(false) ["required"]=> NULL } print_r($_POST); Array ( [food] => Array ( [0] => steak ) [submit] => Submit ) Edited October 6, 2012 by tgavin Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 ["name"]=> string(6) "food[]" See the problem? "food[]" is a 6 character string. $_POST has a key of "food", a 4 character string. Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Author Share Posted October 6, 2012 Right, $_POST "food" is an array, and I need to get the values of that array. Instead it returns an undefined index. That's where I'm having the difficulty. I was testing with a string to make sure info was actually being passed. now i see what that wasn't a good idea. sorry for the confusion. This is basically what I'm trying to accomplish if(isset($_POST[$data['name']])) { foreach($_POST[$data['name']] as $value) { if($_POST[$data['name']] == $data['value']) { $return .= ' checked="checked"'; } } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 Well, you'll have to either store the name value without the [], and add it when you create the checkboxes, or store both versions, or manipulate the string later. There's lots of ways to do it. Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Author Share Posted October 6, 2012 Thank you. i was too close. this solved the problem. $name = rtrim($data['name'],'[]'); Quote Link to comment 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.