Running into some trouble here with a php form and wondering if anyone has any ideas in terms of how to get around it.
I’ve got a list of input boxes that are declared like this:
<?php
$_SESSION["quant1"] = $_POST["quant1"];
echo "<input type=\"text\" name=\"quant1\" size=\"1\" maxlength=\"2\" value=\"" . $quant1 . "\">
?>
Problem is that I have a quant0, quant1, quant2, quant3, etc. The number of items is the size of a csv file.
Now what I would like to do is have it as a for loop so that I can generate n number of the above and adjust n given the size of the csv (which is stored as an array).
I was trying to do the for loop for the input box as an array:
<?php
for ($i = 0; $i <= $sizeOfArray; $i++){
$_SESSION["quant[$i]"] = $_POST["quant[$i]"]; // this won't work!
echo "<input type=\"text\" name=\"quant[$i]\" size=\"1\" maxlength=\"2\" value=\"" . $quant[$i] . "\"> // this won't work either!
}
?>
Anyone have any ideas? Thanks!
You don’t need to define the array indexes in forms. If you give the array of form elements the same name, and use closed brackets after each name, they will automatically be converted to an array when they reach the server.
For example…
<form ... >
<input type="text" name="fruit[]" value="apple"/>
<input type="text" name="fruit[]" value="banana"/>
<input type="text" name="fruit[]" value="cherry"/>
</form>
<?php
echo( $_POST[ "fruit" ][ 0 ] ); // apple
echo( $_POST[ "fruit" ][ 1 ] ); // banana
echo( $_POST[ "fruit" ][ 2 ] ); // cherry
?>
Is that what you are looking for?
- 08 July 2008 12:27 PM
-
Hmm I suppose so. Except the value must be whatever is stored in $_POST[ “fruit” ][ i ]. So would this work?
<form ... >
<input type="text" name="fruit[]" value="fruit[]"/>
<input type="text" name="fruit[]" value="fruit[]"/>
<input type="text" name="fruit[]" value="fruit[]"/>
</form>Code:
<?php
echo( $_POST[ "fruit" ][ 0 ] );
echo( $_POST[ "fruit" ][ 1 ] );
echo( $_POST[ "fruit" ][ 2 ] );
?>
I suppose I can also declare the session as:
$_SESSION['fruit'] = $fruit;
?
- 08 July 2008 03:09 PM
-
That wouldn’t work because all of your posted input values would be “fruit[]”. I took another look at your PHP code and spotted a few errors so I fixed them, see if this works for you now…
<?php
foreach( $quant as $value )
{
$_SESSION[ $value ] = $_POST[ $value ];
echo( "<input type=\"text\" name=\"{$value}\" size=\"1\" maxlength=\"2\" value=\"{$value}\">" );
}
?>
I’m guessing that $quant is an array of values for the form inputs.
- 08 July 2008 03:33 PM
-
Still not quite working. $quant is an array that holds the form inputs yes.
Well, starting back from the beginning, here’s sort of the big picture:
<?php
$_SESSION["quant"] = $_POST["quant"];
// get data
$filestream = file_get_contents("$order_menu1");
$line_separated = explode("\n",$filestream); // separate by line
for ($i = 0; $i < sizeof($line_separated); $i++){
// separate line by item
$comma_separated = explode(",",$line_separated[$i]);
$item[$i] = $comma_separated[0];
$price[$i] = $comma_separated[1];
$description[$i] = $comma_separated[2];
if ($item[$i] != null){
echo "<input type=\"text\" name=\"$quant[$i]\" size=\"1\" maxlength=\"2\" value=\"".$quant[$i]."\"> ";
echo $item[$i]; echo "@";
echo "$"; printf ("%01.2f", $price[$i]);
echo "<br/>";
}
}
?>
Now it works if this line:
echo “<input type=\“text\” name=\”$quant[$i]\” size=\“1\” maxlength=\“2\” value=\”“.$quant[$i].”\”> “;
is changed to this
echo “<input type=\“text\” name=\“quant\” size=\“1\” maxlength=\“2\” value=\”“.$quant.”\”> “;
(i.e. it’s “quant” as opposed to the array “quant”)
Thanks for your help so far Si!
- 09 July 2008 03:59 AM
-
Got it to work using the first post from Si.
If anyone else is interested, here is what I did:
for ($i = 0; $i < sizeof($line_separated); $i++){
// separate line by item
$comma_separated = explode(",",$line_separated[$i]);
$item[$i] = $comma_separated[0];
$price[$i] = $comma_separated[1];
$description[$i] = $comma_separated[2];
if ($item[$i] != null){
echo "<input type=\"text\" name=\"quant[]\" size=\"1\" maxlength=\"2\" value=\"".$quant[$i]."\"> ";
echo $item[$i]; echo "@";
echo "$"; printf ("%01.2f", $price[$i]);
echo "<br/>";
}
}
The part that helped was assigning name=quant[]. Then it worked. ![]()
Thanks Si for your help!
- 09 July 2008 11:20 PM
-
- Log in or join for free to make a comment.


