Karthik
Karthik

Reputation: 1091

HTML Array Element not getting in $_POST

I have a form element named product_id[] its an array. Eg.

<input type="hidden" name="product_id[]" value="1" />
<input type="hidden" name="product_id[]" value="2" />

In our development server when I do

print_r($_POST['product_id']);

I get all my product id values. When I try to do the same in our live server just the string "Array" is printed in the screen. I tried changing ENCTYPE in my form without any luck. When I changed to GET I am getting all my values printed in my URL, but when I do print_r($_GET['product_id']) I am getting same as post. So, i hope this should be something to do with PHP $_POST and $_GET. Is there any extension or module that I am missing in my live server?

Can anyone please shed some light on it?

Thank you,

Upvotes: 1

Views: 2442

Answers (6)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

From your comment:

["product_id"]=> string(5) "Array"

This means that $_POST['product_id'] is a string that contains the word Array. This cannot happen with the code you've posted. My guess is that you are filling a form field called product_id using a complete PHP array, rather than one of its items. E.g.:

$foo = array(1, 2, 3);
echo '<input type="hidden" name="product_id" value="' . $foo . '">';

... which prints:

<input type="hidden" name="product_id" value="Array">

Update:

I'm not sure you fully got my point. Here's a full snippet that reproduces your problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>

<?php

echo '<xmp>'; var_dump($_POST); echo '</xmp>';

?>

<form action="" method="post">
<div>
    <input type="hidden" name="product_id[]" value="1">
    <input type="hidden" name="product_id[]" value="2">

<?php

$foo = array(1, 2, 3);
echo '<input type="hidden" name="product_id" value="' . $foo . '">';

?>
    </div>
    <div><input type="submit" name="Submit"></div>
</form>

</body>
</html>

As I said, the sole code you've shown us cannot have this effect. That's why seeing the full code is so important.

Upvotes: 2

Devesh
Devesh

Reputation: 1

Well guys this is not that weird..if print_r($_POST['product_id']) returnr an array..everything is correct.congrats...... Wat is probably going wrong is the way u hav written the html code..try assigning indexs to product_id..

Upvotes: 0

tacone
tacone

Reputation: 11441

Some implicit Array to string type conversion happens.

If for real your var_dump() works while print_r() does not then it's for sure a PHP bug and you should check the PHP version on your server and update it.

what happens on your live server when you foreach ( $_POST as $v) echo $v; ?

Please confirm that print_r($_POST) and var_dump( $_POST ) are behaving differently.

Upvotes: 0

Furicane
Furicane

Reputation: 1183

Several things could go wrong. The reason why you only get "Array" printed out is because $_POST is empty array. Reasons for this might be the actual misconfiguration of PHP itself, if you have Suhosin installed OR it might be an error at your end. Usually, in my case, it's always my fault :)

  • Check whether you're sending your POST request to correct URL (live one instead of testing one).
  • Check if your hidden fields are actually present before sending them.
  • Check whether you made a trivial mistake with naming your hidden fields

That should at least cover most common mistakes that might occur.

Upvotes: 0

Jon
Jon

Reputation: 437376

So it seems that for some reason your variable is the string "Array" instead of an actual array. This is weird, I haven't seen it before.

As a temporary workaround, you can try using parse_str and see if this exports the value of product_id correctly.

Upvotes: 0

RR_1990
RR_1990

Reputation: 193

What you can try is print the the post like this print_r($_POST), now you can see if the product_id is passed trough

Upvotes: 0

Related Questions