Jump to content

Checkbox array PHP


waynew

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.