0

I have a form like this:

<form action='http://zzz.com' method='post'>
    <input name='fruit[1]' value='apple' />
    <input name='fruit[2]' value='banana' />
</form>

on the server side this should be an array.

assert( $_POST['fruit'] === array(1=>'apple', 2=>'banana') );

In my case it is an empty string !!??!!

assert( $_POST['fruit'] === '' );

I have no idea what the problem is - never seen this before. Anyone can help plz?

PHP 5.5.12

Markus Schober
  • 348
  • 2
  • 7
  • Did you try debugging with `var_dump($_POST);` ? – tillz Jul 18 '14 at 13:06
  • Yes, of course. It's an empty string… – Markus Schober Jul 18 '14 at 13:08
  • Then the form hasn't been serialized properly. Check the actual raw HTTP request to see that form serialized fine. – N.B. Jul 18 '14 at 13:10
  • 2
    May sound stupid but - how do you submit? :-) – thedom Jul 18 '14 at 13:11
  • 2
    http://stackoverflow.com/questions/4543500/ This question looks similar. – scragar Jul 18 '14 at 13:11
  • `var_dump` returns `array(1) { ["fruit"]=> array(2) { [1]=> string(5) "apple" [2]=> string(6) "banana" } }`. No problems there... – Mooseman Jul 18 '14 at 13:12
  • It must be from a tutorial @scragar - lots of copypasta – Jay Blanchard Jul 18 '14 at 13:13
  • If there is really nothing in the `$_POST`-var, try debugging at a lower level: [e.g. with Apache](http://www.cyberciti.biz/faq/apache-mod_dumpio-log-post-data/) – tillz Jul 18 '14 at 13:13
  • @scragar Yep it looks similar. I've found the question you mentioned but the purpose of that question was an other. I've copied the form from this question - thats everything. – Markus Schober Jul 18 '14 at 13:16
  • @JayBlanchard no, it's no a tutorial. Only an example, see comment above ;) – Markus Schober Jul 18 '14 at 13:17
  • @MarkusSchober my point was mostly that that it working fine for everyone else, clearly you are doing something different to everyone else. If you're using exactly the same code in your tests try checking your browsers developer console by pressing F12 and checking the network tab, see if you can see the request headers. – scragar Jul 18 '14 at 13:19
  • the request headers are fine. I guess in my case PHP does something wrong?!?!? I don't know, I've never seen this weird behavior. – Markus Schober Jul 18 '14 at 13:21
  • @MarkusSchober could you try using `var_dump(file_get_contents('php://input'));` to see if PHP is getting the correct headers fed in as input from the webserver? – scragar Jul 18 '14 at 13:24
  • Request headers are fine = content type is url encoded? You might also want to insert the headers in your post to make sure everyone else can see that they are fine as well. :) – monocell Jul 18 '14 at 13:27
  • This is the part of the form data in raw `&location%5B%5D=63&location%5B%5D=58&location%5B%5D=89` and the content type `application/x-www-form-urlencoded`. For me, this looks fine. Or am I missing something? – Markus Schober Jul 18 '14 at 13:41
  • Your location params are escaped. That's the issue, you're not setting `location` as an array, you're setting a param litterally called `location[]` – scragar Jul 18 '14 at 14:08
  • 1
    Ok, I have the error! Another developer has added an other form input with the same name. So there are checkboxes with the name `location[]` and a text input with `location`. I've overseen this in his commit and it worked well with apache on his machine (Mamp Pro). On my computer I'm using vagrant with the Laravel Homestead Box (Nginx) and it seems, Nginx doesn't like this. So thanx for your answers. – Markus Schober Jul 18 '14 at 16:28

1 Answers1

0

use like this...

<form action='' method='post'>
    <input name='fruit[1]' value='apple' />
    <input name='fruit[2]' value='banana' />
    <input type="submit" name="btn" value="OK" />
</form>
<?php
print_r($_POST['fruit']);
?>

after submitting ok button it will show you complete array

Guru
  • 1
  • 1