Ultrashock Forums > Development > Server Side
PHP - POST an array?
Member Blogs
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
PHP - POST an array?
Old 2008-07-08

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:

Code:
<?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:

Code:
<?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!
let ɛ < 0


Ekoik Business Group | www.ekoik.com
postbit arrow 6 comments | 469 views postbit arrow Reply: with Quote   
Andy M
andyMah
Andy M is online now Super Moderator
seperator
Posts: 3,900
2003-09-11
Age: 23
Andy M lives in Canada
Andy M has 13 blog entries13
Andy M's Avatar
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is online now Nutrox lives in United Kingdom 1 Blog Entries 13 Creative Assets 2008-07-08 #2 Old  
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...

Code:
<form ... >
    <input type="text" name="fruit[]" value="apple"/>
    <input type="text" name="fruit[]" value="banana"/>
    <input type="text" name="fruit[]" value="cherry"/>
</form>
Code:
<?php

echo( $_POST[ "fruit" ][ 0 ] ); // apple
echo( $_POST[ "fruit" ][ 1 ] ); // banana
echo( $_POST[ "fruit" ][ 2 ] ); // cherry

?>
Is that what you are looking for?

Reply With Quote  
Andy M's Avatar Andy M Andy M is online now Super Moderator Andy M lives in Canada 13 Blog Entries 2008-07-08 #3 Old  
Hmm I suppose so. Except the value must be whatever is stored in $_POST[ "fruit" ][ i ]. So would this work?

Code:
<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:

Code:
$_SESSION['fruit'] = $fruit;
?
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is online now Nutrox lives in United Kingdom 1 Blog Entries 13 Creative Assets 2008-07-08 #4 Old  
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...

Code:
<?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.
Reply With Quote  
Andy M's Avatar Andy M Andy M is online now Super Moderator Andy M lives in Canada 13 Blog Entries 2008-07-08 #5 Old  
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:

Code:
<?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!
Reply With Quote  
Andy M's Avatar Andy M Andy M is online now Super Moderator Andy M lives in Canada 13 Blog Entries 2008-07-09 #6 Old  
Got it to work using the first post from Si.

If anyone else is interested, here is what I did:

Code:
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!
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is online now Nutrox lives in United Kingdom 1 Blog Entries 13 Creative Assets 2008-07-09 #7 Old  
Hehe... I'm glad you got that working Andy, I have to admit it was confusing me a little.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: