Form Input name with square brackets not an array in PHP

578

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

630

Answer

Solution:

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

People are also looking for solutions to the problem: php - Rewrite directorys to file and after ? variables

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.