6

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

16
  • 7
    Try name="om[]" on each (without keys) Commented Jul 30, 2013 at 10:39
  • Try $om = $_POST['om'] then access the indices. I think that the print_r is confusing you. Commented Jul 30, 2013 at 10:41
  • 3
    @FDL there is nothing wrong with providing keys Commented Jul 30, 2013 at 10:43
  • 1
    @sandeepKumar can you show your form tag Commented Jul 30, 2013 at 10:45
  • 1
    What has changed when it stopped working? A new PHP version, server reconfiguration, ...? Can you post your variables_order and request_order settings from the server? (You can use ini_get() to read them if you don't have direct configuration access.) Commented Aug 11, 2013 at 0:26

11 Answers 11

3

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

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

Comments

2

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
)

2 Comments

it's runing good for me on my server earlier also,it's just i think server problem that those values which are in array not able to access it with $_POST but can access with $_REQUEST
@sandeepKumar you should copy/paste the above test code in a PHP file on your server, to see if the same behavior occurs. This way you will be able to tell if it is a server/vhost level issue, or something in your PHP stack.
2
+50

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.

5 Comments

i have faced this fatal error on my server PHP Fatal error: Allowed memory size of 94371840 bytes , then i have did this ini_set('memory_limit', '-1'), leaving this nothing has changed on server
Well the memory consumption is a sign that you hit some boundary. I would further try to understand why you hit the memory limit. disabling memory limit only hides the error but does not stop the cause. so it removes the symptom only.
you are absolutely right,but when i see the fatal error in error log, i found this solution only for that moment to keep running server, can you please suggest what's wrong with $_POST, it's givng values if the post value is not an array , but when it's a arryay it's just returning a string "Array"
You need to trouble-shoot the issue. I can't tell you because I can not reproduce it nor do I have access to the code nor server. You probably need to find someone you can go through your issue so you've got a fresh pair of eyes? Nobody there where you are who can help you?
i have tried with all then only i posted on stackoverflow,anyways thank you for your precious time :)
1

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

Comments

0

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

Comments

0

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

Comments

0

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
     )

Comments

0

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.

Comments

0

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.

2 Comments

If he does not have a method="post" - would be an empty array $_POST
Yes you does not have a method="post" then form taken method="get" by default. please mention method="post" attribute in form tag. If you not add attribute method="post" then the $_POST not working. It will $_GET or $_REQUEST
0

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)

Comments

0

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);

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.