Jump to content

PHP5 - Problem with array submitted from HTML form


Guest Tim_Christopher

Recommended Posts

Guest Tim_Christopher

Hi,

 

I've got a basic web form something like:

 

<form action=".." method="$_POST">
   <input type="checkbox" name="userId[]" value="1" />
   <input type="checkbox" name="userId[]" value="2" />
   <input type="checkbox" name="userId[]" value="3" />
   <input type="submit" name="submit" value="Click Here" />
</form>

 

The following is what I would expect to happen assuming all 3 were checked:

 

$_POST = array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" }

$_REQUEST = array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" }

 

The actual output is:

 

$_POST = string(5) "Array"

$_REQUEST = array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" }

 

Can anyone explain this?..  The code used to work however I have since upgraded to PHP5 and there may have been alternations in the php.ini file.

 

I know I could just go through changing every instance of $_POST to $_REQUEST but it just doesn't feel right, and I'd rather fix whatever has changed.

 

Any help would be appreciated  ;D

 

Guest Tim_Christopher

Yeah - that was just a typo on my post, but I can't find an edit button.

 

It should actually read:

 

<form action=".." method="POST">
   <input type="checkbox" name="userId[]" value="1" />
   <input type="checkbox" name="userId[]" value="2" />
   <input type="checkbox" name="userId[]" value="3" />
   <input type="submit" name="submit" value="Click Here" />
</form>

How exactly are you printing these results. Using...

 

<form method="POST">
   <input type="checkbox" name="userId[]" value="1" />
   <input type="checkbox" name="userId[]" value="2" />
   <input type="checkbox" name="userId[]" value="3" />
   <input type="submit" name="submit" value="Click Here" />
</form>
<?php

    if (isset($_POST['submit'])) {
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
    }  

?>

 

this code (and ticking all options), produces....

 

Array
(
    [userId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [submit] => Click Here
)

 

for me.

Guest Tim_Christopher

Or for a more complete example the following code:

<form method="POST">
   <input type="checkbox" name="userId[]" value="1" />
   <input type="checkbox" name="userId[]" value="2" />
   <input type="checkbox" name="userId[]" value="3" />
   <input type="submit" name="submit" value="Click Here" />
</form>
<?php

    if (isset($_POST['submit'])) {
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
    }
    if (isset($_POST['submit'])) {
        echo '<pre>';
        print_r($_REQUEST);
        echo '</pre>';
    }
?>

 

Produces:

 

Array
(
    [userId] => Array
    [submit] => Click Here
)

Array
(
    [userId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [submit] => Click Here
)

try this please.

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="checkbox" name="userId[]" value="1" />
   <input type="checkbox" name="userId[]" value="2" />
   <input type="checkbox" name="userId[]" value="3" />
   <input type="submit" name="submit" value="Click Here" />
</form>
<?php

    if (isset($_POST['submit'])) {
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
    }  

?>

  • 2 weeks later...

That is very strange. What appears to be happening is that print_r is not iterating through the array, only staying at level 1. I use and script with PHP 5 and the data completely prints out for myself as well.

 

try doing a var_dump($_POST) and see what comes out.

Guest Tim_Christopher

The problem is not with print_r - it's with the $_POST array.

 

In the $_REUQEST array: The variable $_REQUEST['userId'] is an array, .e.g. is_array(..) returns true.

 

In the $_POST array: The variable $_POST['userId'] is a string containing the text 'Array'.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.