sandeepKumar
sandeepKumar

Reputation: 811

$_POST is returning just 'Array' as string and i am posting an Array

It's a very strange error that i am facing.I have some html i.e below

<input type="checkbox"  name="om[1]" value="10">
<input type="checkbox"  name="om[2]" value="20">
<input type="checkbox"  name="om[3]" value="30">

When i post this form and do

print_r($_POST['om'])
it gives just prints 'Array' as string
and if i do print_r($_POST['om'][0]) it gives A
and if i do print_r($_POST['om'][1]) it gives r 

But if i do

print_r($_REQUEST['om'])
it display a proper array

Array
   (
    [1] => 10
    [2] => 20
    [3] => 30
  )

Problem is when i am using $_POST for getting array values it not displaying, it works fine if posted value is not in array. But i can get all the required result with $_REQUEST['om'] even if they are array.

And it's happening only on server, working fine for localhost. Can anybody tell what can be the problem on server??

Upvotes: 6

Views: 4763

Answers (11)

Akshay S
Akshay S

Reputation: 1

I was trying to pass array in $_POST using curl and on dumping, it was showing "Array" of type string to me. I found this question while looking for a solution. I hope this works out for your problem. The solution is to create a single array of parameters and then pass it as query as follows:

$aPostArray['aMyArray']=$aMyArray;

$sFieldString = http_build_query($aPostArray);

and then set it as

curl_setopt($ch1, CURLOPT_POSTFIELDS,$sFieldString);

Upvotes: 0

Madra David
Madra David

Reputation: 151

Checkboxes , Check them and you will get your values

<input type="checkbox"  name="om[1]" value="10" checked="true">
<input type="checkbox"  name="om[2]" value="20" checked="true">
<input type="checkbox"  name="om[3]" value="30" checked="true">

When a form is submitted, only "on" checkbox controls can become successful. From the spec (4.01)

Upvotes: 0

user2684182
user2684182

Reputation:

If you try to print_r an Array, it'll return Array. Most data types in PHP are like this.

Upvotes: 1

sandeepKumar
sandeepKumar

Reputation: 811

The answer is put your magic_quotes_gpc = off in php.ini file

Upvotes: 3

Chinmay235
Chinmay235

Reputation: 3414

Hello i have checked above your code. It working fine. I think you are missing the form submit method

Please use method="post"

You can use indexing array like om[1],om[2],om[3] it doesn't matter.

Here is code:

<form method="post">
 <input type="checkbox"  name="om[1]" value="10">
 <input type="checkbox"  name="om[2]" value="20">
 <input type="checkbox"  name="om[3]" value="30">
 <input type="submit" name="submit" value="Submit">
</form>

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

Please check this code.

Upvotes: 0

Chuck
Chuck

Reputation: 1210

It seems like $_POST['om'] has been converted to a string accidentally. Like print_r($_POST['om'] . "") Missing debug statements?

I think the problem lies somewhere in a piece of code that you're not showing here. Nevertheless, you should use var_dump more often to analyse variables.

Upvotes: 0

krishna kinnera
krishna kinnera

Reputation: 1543

name="om[]"- remove the keys and try it's working for me.

    <pre>
    <form method="post">
    <input type="checkbox"  name="om[]" value="10">
    <input type="checkbox"  name="om[]" value="20">
    <input type="checkbox"  name="om[]" value="30">
    <input type="submit">
    </form>
    <?php
    print_r($_POST['om']);
    print_r($_REQUEST['om']);
    ?>

Output:

    Array
     (
        [0] => 10
        [1] => 20
        [2] => 30
     )

    Array
     (
        [0] => 10
        [1] => 20
        [2] => 30
     )

Upvotes: 0

hakre
hakre

Reputation: 197767

Can anybody tell what can be the problem on server?

Most likely your own can because you has access to it and you can trouble-shoot it.

From what you describe, on your server $_POST['om'] is the string "Array". Period.

How it has become that is not visible from the code you've posted in your question, only that it is.

BTW, the string "Array" is also a sign that on the server you (probably by accident) have placed code and configuration that casts an array to a string (http://php.net/language.types.type-juggling).

A modern PHP version will notice you about such btw. So first thing you should do is enable error logging on the server and set the verbosity to the highest level so that you can see warnings, notices and strict warnings in the PHP error log. Then follow the error log and look for array conversion notices.

If that does not lead you to anything, you need to get a step-debugger in your hands and do some remote debugging so to verify that your expectations are met and where they get broken. That normally is the the fastest way to find out what happens as you can inspect the program while it runs.

Upvotes: 2

cske
cske

Reputation: 2243

http://php.net/manual/en/function.print-r.php prints string "Array" only if the parameter is string, so I guess the $_POST superglobal was overridden before the print_r line (which is also proven by the fact that the $_RESQUEST suberglobal contains the original and expected value) try to var_export it

Upvotes: 0

user1646111
user1646111

Reputation:

This is just explanation, I tested this:

<pre>
<form method="post">
<input type="checkbox"  name="om[1]" value="10">
<input type="checkbox"  name="om[2]" value="20">
<input type="checkbox"  name="om[3]" value="30">
<input type="submit">
<?php
print_r($_POST['om'][1]);
print_r($_POST['om'][2]);
print_r($_POST['om'][3]);

print_r($_REQUEST['om']);
?>

Output:

10
20
30
Array
(
    [1] => 10
    [2] => 20
    [3] => 30
)

Upvotes: 2

mgrueter
mgrueter

Reputation: 1420

I'm pretty sure that

<input type="checkbox"  name="om[1]" value="10">

is not the same as

$_POST['om'][1]

The one in the form is an array with a string-type index "1", while in the $_POST var you're accessing the first element of $_POST['om'].

So you'll either have to use something else than integers in the HTML form or access the values by using $_POST['om']['1'].

Upvotes: 0

Related Questions