Jump to content

[SOLVED] $_POST is different from $_REQUEST


Xeoncross

Recommended Posts

Has anyone ever had a PHP script that had different values in the $_POST array than in the $_REQUEST array?

Take a look at the values below and you will see that data sent through a method="POST" form became different for both arrays.

 

 

 

print_r($_POST); - inputs became a string

Array
(
    [inputs] => Array
    [action] => disable
    [submit] => submit
    [active] => 1
)

 

 

 

print_r($_REQUEST); inputs became an array

Array
(
    [active] => 1
    [inputs] => Array
        (
            [0] => 65
            [1] => 64
        )

    [action] => disable
    [submit] => submit
)

Link to comment
Share on other sites

$_REQUEST and $_POST the same here!

Array
(
    [txtbox] => foo
    [inputs] => Array
        (
            [0] => b
            [1] => c
        )

    [submit] => submit
)

 

Array
(
    [txtbox] => foo
    [inputs] => Array
        (
            [0] => b
            [1] => c
        )

    [submit] => submit

Not sure what you mean?

 

Test code:

<?php


echo '<h1>$_REQUEST</h1>
<pre>' . print_r($_REQUEST, true) . '</pre>

<h1>$_POST</h1>
<pre>' . print_r($_POST, true) . '</pre>';

?>
<form action="#" method="post">
  Text: <input type="text" name="txtbox" /><br />
  Inputs:<br />
  <input type="checkbox" name="inputs[]" value="a" /> A<br />
  <input type="checkbox" name="inputs[]" value="b" /> B<br />
  <input type="checkbox" name="inputs[]" value="c" /> C<br />
  Submit: <input type="submit" name="submit" value="submit" />
</form>

Link to comment
Share on other sites

Ok, I just striped my code down to something simple like the below code - and it works.

So my guess is that somewhere in my system something is messing with the $_POST data and turning it into a string. (the only thing is that I can't find anywhere where my code messes with the $_POST data!)

 

So any ideas? 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>POST and REQUEST TEST</title>

<script type="text/javascript">

// Check All Code from 
// http://www.dustindiaz.com/check-one-check-all-javascript/

function checkAllFields(ref)
{
var chkAll = document.getElementById('checkAll');
var checks = document.getElementsByName('inputs[]');
var removeButton = document.getElementById('submit');
var boxLength = checks.length;
var allChecked = false;
var totalChecked = 0;
if ( ref == 1 )
{
	if ( chkAll.checked == true )
	{
		for ( i=0; i < boxLength; i++ )
		checks[i].checked = true;
	}
	else
	{
		for ( i=0; i < boxLength; i++ )
		checks[i].checked = false;
	}
}
else
{
	for ( i=0; i < boxLength; i++ )
	{
		if ( checks[i].checked == true )
		{
		allChecked = true;
		continue;
		}
		else
		{
		allChecked = false;
		break;
		}
	}
	if ( allChecked == true )
	chkAll.checked = true;
	else
	chkAll.checked = false;
}
for ( j=0; j < boxLength; j++ )
{
	if ( checks[j].checked == true )
	totalChecked++;
}
removeButton.value = "Process ["+totalChecked+"] Selected";
}

</script>

</head>

<body>

<?php
echo '<h1>$_REQUEST</h1>
<pre>' . print_r($_REQUEST, true) . '</pre>

<h1>$_POST</h1>
<pre>' . print_r($_POST, true) . '</pre>';

?>

<form action="#" method="post" onsubmit="return confirm('Are you sure you want to ' + document.getElementById('action').value + ' the selected items?');">
    
    <fieldset>
        <legend></legend>

        <dl>
            <dt>
                <label for="title">
                    <input class="boxes" value="67" name="inputs[]" onclick="checkAllFields(2);" type="checkbox">
                </label>
            </dt>
            <dt>
                <label for="title">
                    <input class="boxes" value="68" name="inputs[]" onclick="checkAllFields(2);" type="checkbox">
                </label>
            </dt>
            <dt>
                <label for="title">
                    <input class="boxes" value="69" name="inputs[]" onclick="checkAllFields(2);" type="checkbox">
                </label>
            </dt>
            <dt>
                <label for="title">
                    <input class="boxes" value="70" name="inputs[]" onclick="checkAllFields(2);" type="checkbox">
                </label>
            </dt>
            
            <dt>
                <label for="action">Action to take:</label>
            </dt>
            <dd>
                
                <select name="action" id="action">
                    <option selected="selected" value="approve">Approve</option>

                    <option value="disable">Disable</option>
                    <option value="delete">Delete</option>
                </select>
            </dd>
            
            <dt>
                <label for="removeChecked"></label>
            </dt>
            <dd class="submit">
                <input value="Process [0] Selected" name="submit" id="submit" type="submit">
                <input value="1" name="active" type="hidden">
            </dd>
            
        </dl>
    </fieldset>
</form>

 

Again, the simplified code above works just fine.

 

Link to comment
Share on other sites

Ok, I found it.

 

I was cleaning the POST data somewhere like this:

 

<?php
if (get_magic_quotes_gpc()) {
    //foreach $_GET variable
    foreach($_GET as $key => $value) {
        //Clean the value of the added slashes
        $_GET[$key] = stripslashes($value);
    }
    //foreach $_POST variable
    foreach($_POST as $key => $value) {
     
        //Clean the value of the added slashes
        $_POST[$key] = stripslashes($value);
    }
}
?>

 

Which is bad because I had a multi-level $_POST array ($_POST['inputs'][0] was on level 3). So I was messing myself up by trying to clean the forward slashes from the posted code. aghhhhh!

 

 

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.