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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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>';
    }  

?>

Link to comment
Share on other sites

Guest Tim_Christopher

The output if any of the boxes are checked:

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

 

Or if none are selected:

Array
(
    [submit] => Click Here
)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'.

 

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.