waynew Posted June 12, 2011 Share Posted June 12, 2011 Ok, I've never had this problem before. Everything is working perfectly on my local machine (PHP 5.3.0). I have a checkbox array that looks like this: <input type="checkbox" name="games[]" value="<?php echo $key; ?>" <?php echo $checked; ?>/><?php echo $val; ?><br /> It's printing out perfectly in the source on both my local machine and the two free servers I've just uploaded it to. However, on the two free servers I've uploaded my script to, they don't seem to be getting the array after the form is submitted. On my local machine, it's working perfectly and my script is recognizing the array. When I do a print_r on games, it simply just prints out Array, even if you've selected some of the boxes. On the two servers, it doesn't even print out as Array( ). In my script, I check to make sure that the user has checked at least one game. (Bear in mind that the $values array is simply just the $_POST array after being passed through a class function. if(!isset($values['games']) || sizeof($values['games']) == 0 || !is_array($values['games'])){ $this->util->add_error("Please select what games you usually play."); } No matter how many checkboxes I check, I still get the error above: "Please select what games you usually play." As I said, on my local machine, all of this is working fine and I don't get that error. Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2011 Share Posted June 12, 2011 It's a fairly safe bet that your code is doing something that isn't working on the two servers. Without enough of your code that reproduces the problem, from the form through to the point where the data doesn't make it, it's not possible to eliminate your code as the cause. Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1228846 Share on other sites More sharing options...
waynew Posted June 12, 2011 Author Share Posted June 12, 2011 Thanks for the reply! The xhtml in my form looks like this (this is from the source): <input type="checkbox" name="games[]" value="1" />Call of Duty 2<br /> <input type="checkbox" name="games[]" value="2" />Call of Duty 3<br /> <input type="checkbox" name="games[]" value="0" />Call of Duty 4<br /> <input type="checkbox" name="games[]" value="4" />Call of Duty Black Ops<br /> <input type="checkbox" name="games[]" value="3" />Call of Duty MW2<br /> <input type="checkbox" name="games[]" value="6" />Call of Duty MW3 (Not released)<br /> <input type="checkbox" name="games[]" value="5" />Call of Duty WAW<br /> When the form is submitted, my page handles the POST data and sends it to a class function: if(isset($_POST['submit_gt'])){ $insert = $user->add_gamertag($_POST); if($insert){ $util->redirect('submit.php?m=gt_submitted'); } } Then, my class function basically checks to see if at least one checkbox has been checked: $chosen_games = array(); if(!isset($values['games']) || sizeof($values['games']) == 0 || !is_array($values['games'])){ $this->util->add_error("Please select what games you usually play."); } else{ foreach($values['games'] as $val){ if(array_key_exists($val, $games)){ $chosen_games[] = $val; } } } I keep getting the "Please select what games you usually play." error, regardless of how many checkboxes I check. Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1228850 Share on other sites More sharing options...
waynew Posted June 13, 2011 Author Share Posted June 13, 2011 Would this have anything to do with the fact that I have an array called $games? Edit: No. Just changed the name of the checkbox array to something else. print_r is still just printing out Array Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1228866 Share on other sites More sharing options...
Psycho Posted June 13, 2011 Share Posted June 13, 2011 Instead of doing a print_r() on $games, do a print_r() on $_POST to validate that the data is getting passed correctly Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1228873 Share on other sites More sharing options...
waynew Posted June 13, 2011 Author Share Posted June 13, 2011 I did a print on the POST variables. It shows like so: Array ( [username] => tester [platform] => 0 [gamertag] => testingthis [favourite_time_of_play] => Evening [timezone] => 3 [uses_mic] => 1 [password] => tester123 [password_repeat] => tester123 [games] => Array [gametypes] => Array [code] => P2KW [submit_gt] => Submit Details ) Note that my checkbox arrays are games and gametypes. Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1229009 Share on other sites More sharing options...
PFMaBiSmAd Posted June 13, 2011 Share Posted June 13, 2011 This reminds me of a recent thread where someone had written a custom framework (that he didn't bother to mention, show, or to remember that it was doing something to all his post data) that was pre-processing all the $_POST variables and he had a problem with arrays. Do you have anything going on in the code like a framework or code in a prepended file that could be looping through all the $_POST data, but is only doing it in one dimension, so that if an array existed in the data it would be truncated? Perhaps some code that detects magic_quotes_gpc and runs conditionally only on some servers? Another possibility is if you have the stupid suhosin hardened php patch installed and the suhosin.post.max_array_depth has been set to zero. Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1229074 Share on other sites More sharing options...
waynew Posted June 13, 2011 Author Share Posted June 13, 2011 Holy crap! That never crossed my mind. I do indeed use a custom framework and it does have a piece of code that attempts to reverse the effects of magic quotes if they're detected. My local server doesn't have magic quotes enabled. The two servers that the code isn't working on does. I'm pretty positive that must be the problem. Thanks for the help. I was completely stumped on this. Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1229086 Share on other sites More sharing options...
PFMaBiSmAd Posted June 13, 2011 Share Posted June 13, 2011 magic_quotes (and all the other things php put into the language to HELP people write 'working' code) has wasted a HUGE amount of man hours and processor cycles. Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1229104 Share on other sites More sharing options...
Psycho Posted June 13, 2011 Share Posted June 13, 2011 The manual does include a process to properly handle cleansing input that may be affected by magic quotes - including sub-arrays: http://www.php.net/manual/en/security.magicquotes.disabling.php See example #2 Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1229171 Share on other sites More sharing options...
PFMaBiSmAd Posted June 13, 2011 Share Posted June 13, 2011 Or more simply (remove the mysql_real_escape_string line if you only want to undo what magic_quotes_gpc does) - <?php function escape_deep(&$item, $key){ if(get_magic_quotes_gpc()){ $item = stripslashes($item); } $item = mysql_real_escape_string($item); } array_walk_recursive($_POST,'escape_deep'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1229173 Share on other sites More sharing options...
Psycho Posted June 13, 2011 Share Posted June 13, 2011 Or more simply (remove the mysql_real_escape_string line if you only want to undo what magic_quotes_gpc does) - <?php function escape_deep(&$item, $key){ if(get_magic_quotes_gpc()){ $item = stripslashes($item); } $item = mysql_real_escape_string($item); } array_walk_recursive($_POST,'escape_deep'); ?> You know, I was looking at that linked page again and trying to figure out why the example function was unsetting the array elements. Then looking at the first user submitted comment I remembered why: magic quotes are applied to the keys as well as the values. So, you need to run strip slashes against both the keys and the values, but I don't think you can change a key value - you need to unset the current one and recreate a new one with the modified value? Quote Link to comment https://forums.phpfreaks.com/topic/239173-checkbox-array-php/#findComment-1229197 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.