Page 1 of 1
arrays and form input
Posted: Wed Apr 02, 2008 8:11 pm
by cutthroat
Can i retrieve data from a forms input boxes directly into an array. i.e
Code: Select all
<?php
$iData=$_POST['inputData'];
$sz=sizeof($iData);
//ect ect
?>
This code works with checkboxes, but not with input boxes. Am i missing something.
Re: arrays and form input
Posted: Wed Apr 02, 2008 8:38 pm
by John Cartwright
Code: Select all
<input name="foo[]" type="checkbox" value="1">
<input name="foo[]" type="checkbox" value="2">
<input name="foo[]" type="checkbox" value="3">
Code: Select all
echo '<pre>';
print_r($_POST['foo']);
echo '</pre>';
So yes, you can specify the input as an array.
Re: arrays and form input
Posted: Wed Apr 02, 2008 8:52 pm
by cutthroat
Sorry i never made myself clear. I have already done this with checkboxes, can i do the same with a text input box.
I have a series of text input boxes and have entered them like this.
Code: Select all
<?php
$price[0] = $_POST['pr1'];$price[1] = $_POST['pr2'];$price[2] = $_POST['pr3'];$price[3] = $_POST['pr4'];
$price[4] = $_POST['pr5'];$price[5] = $_POST['pr6'];$price[6] = $_POST['pr7'];$price[7] = $_POST['pr8'];
?>
and then dereferenced the data. It would be nice if the checkbox way worked for Text input boxes. Thanks for your quick response anyway.
Re: arrays and form input
Posted: Wed Apr 02, 2008 8:55 pm
by John Cartwright
Yes it's possible. In fact, I arrange my forms using arrays nearly always.
You example is confusing.. can you post your form?
Did you try a print_r() on the data to see how it is organized?
Re: arrays and form input
Posted: Wed Apr 02, 2008 9:09 pm
by cutthroat
here's the relevent bit of html
Code: Select all
<form action="process.php" method = "post">
Price 1. <input type="text" name="pr1" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
Price 2. <input type="text" name="pr2" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
ect ect
</form>
Then i use the posted code above to extract in process.php. Hope this is off help. I tried naming pr1,pr2 ect, name="pr[]", but it did'nt seem to work.
Re: arrays and form input
Posted: Wed Apr 02, 2008 9:11 pm
by John Cartwright
I don't see you assigning the the elements as arrays.
<input name="foobar[]" ... >
Notice the [] .. this means we want it as an element of the array (in the order we put them in unless we specify it a key). And remember, you can go deeper than 2 dimensions.
<input name="data[user][profile]" ... >
Re: arrays and form input
Posted: Wed Apr 02, 2008 9:16 pm
by cutthroat
Sorry i re-edited my post after you replied. I obviously need to slow down and step back. I will play around with a form with some data in it until i get the jist. Thanks for letting me no it is possible. And more dimensions, this could make life easy.

Re: arrays and form input
Posted: Wed Apr 02, 2008 9:28 pm
by John Cartwright
I don't believe you did a print_r() after changing your names to include the array indices
Code: Select all
<form action="process.php" method = "post">
Price 1. <input type="text" name="pr[1]" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
Price 2. <input type="text" name="pr[2]" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
ect ect
</form>
If you leave both of the names to pr[] like in my other example, then the keys will start counting from 0, 1, 2, 3, and so forth. I think you expected it to start from 1. In the example I just posted above, you can specify what you want the key to be, which is what you supposedly want.
Code: Select all
echo '<pre>';
print_r($_POST['pr']); //array(1 => "", 2 => "")
echo '</pre>';
Re: arrays and form input
Posted: Wed Apr 02, 2008 9:34 pm
by cutthroat
Bang on, you got it. Problem solved. The little things in life that get you down. Thanks for your help.

Re: arrays and form input
Posted: Wed Apr 02, 2008 9:40 pm
by John Cartwright
cutthroat wrote:Bang on, you got it. Problem solved. The little things in life that get you down. Thanks for your help.

Excellent. Enjoy the new found knowledge
