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? Link to comment https://forums.phpfreaks.com/topic/269164-weird-array-issue/ 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. Link to comment https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383232 Share on other sites More sharing options...
tgavin Posted October 6, 2012 Author Share Posted October 6, 2012 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 ) Link to comment https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383234 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. Link to comment https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383235 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"'; } } } Link to comment https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383239 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. Link to comment https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383240 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'],'[]'); Link to comment https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.