...

View Full Version : Getting an Array from an HTML Form



DDaku
08-31-2009, 09:19 PM
Based on http://www.php.net/manual/en/faq.html.php#faq.html.arrays I am of the understanding that I should be able to create multiple INPUT elements in an HTML form with the same NAME attribute (plus brackets) and have these elements all appear as one array in the $_POST object in PHP.

To test this, I've created the following .php page:


<html>
<body>
<?php
var_dump($_POST);
?>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
foo[0]: <input type='text' name='foo[]' /><br />
foo[1]: <input type='text' name='foo[]' /><br />
bar[0]: <input type='text' name='bar[]' value='value1' /><br />
bar[1]: <input type='text' name='bar[]' value='value2' /><br />
<input type='submit' />
</form>
</body>
</html>


Simply hitting Submit on this page (without populating either foo field, or changing either bar field), I get the following as the result of the var_dump($_POST):

array(2) { ["foo"]=> string(0) "" ["bar"]=> string(6) "value2" }

Likewise, if I populate both foo INPUT fields, the value for $_POST["foo"] will just be a string containing the last INPUT element with NAME attribute of "foo[]", not an array containing both "foo[]" inputs.

Any ideas what I'm doing wrong?

Thanks in advance.

funnymoney
08-31-2009, 09:28 PM
Any ideas what I'm doing wrong?

Thanks in advance.

well their names are the same

DDaku
08-31-2009, 09:31 PM
well their names are the same

From the PHP docs article I linked at the top of my original post:



To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />

Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />

This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script.

hinch
08-31-2009, 09:37 PM
don't hold me to this but i'm fairly sure you can't array[] text elements they only work on <select> elemments/ check boxes / radiobuttons

Fumigator
08-31-2009, 10:44 PM
don't hold me to this but i'm fairly sure you can't array[] text elements they only work on <select> elemments/ check boxes / radiobuttons

Nah it works on <input> tags, type=text or whatever...

DDaku I didn't see anything out of place just visually scanning through. What browser are you using and can you try this on another? What PHP version? Maybe your HTML isn't valid and that's causing a problem? Not sure...

Try this sample, see if it works on your install. I just ran it using Firefox 3.5 and it worked fine.



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="HAPedit 3.1">
</head>
<body>
<div>
<?php echo "<pre>".print_r($_POST,true)."</pre>"; ?>
</div>
<form method="post">
<input type="text" name='test[]' /><br />
<input type="text" name='test[]' /><br />
<input type="text" name='test[]' /><br />
<input type="text" name='test[]' /><br />
<input type="submit" value="Submit me" />
</form>
</body>
</html>

_Aerospace_Eng_
08-31-2009, 11:12 PM
Your previous code seems to work fine. Its giving you the type of input as well as its length. If you want the actual values then do what Fumigator showed you.

Fumigator
08-31-2009, 11:32 PM
Your previous code seems to work fine. Its giving you the type of input as well as its length. If you want the actual values then do what Fumigator showed you.

The vardump should show foo and bar as arrays, not strings. I prefer print_r() but both would work for this purpose.

DDaku
09-01-2009, 01:09 PM
Well thanks greatly Fumigator, at least somebody understood what I was trying to do :)

The good news is it works now, the bad news is I didn't change anything (just created a new test page with Fumigator's code, ran it, and then re-ran my test page). I suppose it must have been a caching problem or something (although I swear I was clearing that).

Regardless, the problem has gone away, so thanks for taking the time to look into it, I really appreciate the sanity check if nothing else.

Fumigator
09-01-2009, 04:22 PM
You're welcome... I appreciate the well constructed question, especially the reference to material you were studying which indicated you were doing the leg work before coming here! (Sadly, so many people don't even know how to read a manual and come here looking for help)

ilight
09-11-2009, 04:53 AM
a similar doubt. can u please help?

Im trying to get multiple occurances of ec and amt to be stored in arrays with those names and then i have to send them to mysql. So i used arrays in html and tried to get them thru $_REQUEST. but it doesnt work. any help?

(How to retrieve the html array data into php?)


function getdata(){
$area = $_REQUEST["area"];
$month = $_REQUEST["month"];
$entries = $_REQUEST["entries"];
for($i=0;$i<$entries;$i++)
{
?>
<form method="post">
Employee code : <input type="text" name='ec[]'/>
Deduction amount : <input type="text" name='amt[]'><br/>
</form>
<?php
}
$ecarray = array($_REQUEST["ec"]);
$amtarray = array($_REQUEST["amt"]);
echo "<form><input type='submit' value='submit'/><br></form>";
return 1;
}


maybe im missing something very basic :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum