1

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,

9
  • 3
    Try doing a var_dump. What's the output? Commented Mar 9, 2011 at 10:48
  • Minor typo in your print_r line, missing a quote. Commented Mar 9, 2011 at 10:48
  • Is your browser Google Chrome by chance? Commented Mar 9, 2011 at 10:49
  • print_r never outputs the string "Array". Maybe you wrote just print by mistake. Commented Mar 9, 2011 at 10:51
  • No I use firefox and I do the same thing in my development server it works fine but not in our live server.Exactly same code. Commented Mar 9, 2011 at 10:52

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No I checked that as well Vicario. Even if it prints Array in value it should give me an array('arrays') because i clearly have product_id[] in name. I tried creating a small app in both servers. It works in both servers I will continue to check for my error and post the answer here. Thank you for your help..
@Karthik Please see my updated answer. BTW, I suggest you inspect your rendered HTML prior to submission.
0

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

Comments

0

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.

1 Comment

Hi jon I tried parse_str($_POST['product_id]) the result I am getting is "1" I think the I am prepare a small app in my test server and in my live server in order for better understanding.
0

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.

Comments

0

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.

3 Comments

It starts spliting string "Array" in to each character.
so on your live server you have implicit Array to String conversion. Make sure that the code is really the same on both environment (if needed, isolate a proof of concept where you can be 100% sure). Also tell us your PHP version.
Hi Tacone, I tried creating a seprate app with the same kind of set up and it seems to be working in both servers. I think it would be my code which i need to debug. I will post my answer once I find it. Thank you for your help.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.