Forum Moderators: coopster

Message Too Old, No Replies

How to use foreach to grab post data?

         

m3n9

5:09 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



Hi, how do i use foreach to grab post data.

For example, item_ID: s001 , quantity: 5
s002 , 2
s003 , 4

this is code for 1.php
<?
echo "<td align='center'>$item_ID<input type='hidden' name='$item_ID' value='$item_ID'; /></td>";
echo "<td align='center'>$dol_quantity</td>";
echo "<td align='center'>$dol_status</td>";
?>

<select name="$item_ID">
<?php

for ($i=1; $i<=10; $i++) {
echo "<option value='$i'>$i</option>";
} ?>

</select>

this is code for 2.php

foreach( $_POST as $key => $value){

echo $value;
}

eelixduppy

5:41 pm on Dec 23, 2008 (gmt 0)



Hello and Welcome to WebmasterWorld!

The code that you have in 2.php should do what you want and is correct. I'm not exactly sure, then, what you are asking? I'm assuming you are having trouble making 1.php into a form that is correctly submitted to 2.php?

[edited by: eelixduppy at 6:03 pm (utc) on Dec. 23, 2008]

m3n9

5:59 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



I have some problem here

<select name="$item_ID">
<?php

for ($i=1; $i<=10; $i++) {
echo "<option value='$i'>$i</option>";
} ?>

</select>

If there are 4 items then i have to select the quantity for each item.

When post to 2.php, only the last row of the quantity is shown.

How to do i show out the 4 quantities?

eelixduppy

6:15 pm on Dec 23, 2008 (gmt 0)



I think I see. Made up a function real quick:

pPOST($_POST);
#
function pPOST($arr) {
foreach($arr as $key => $value) {
if(is_array [php.net]($value)) {
echo $key . " => \n";
pPOST($value);
} else {
echo $key . ' => '. $value . "\n";
}
}
}

See what that outputs. :)

m3n9

6:38 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



C001 => C001 quantity => 1 H001 => H001 L001 => L001 S001 => S001 submit => submit

o.o?

eelixduppy

6:53 pm on Dec 23, 2008 (gmt 0)



The function iterates through all the values using a foreach loop. :)

Looks like your form is not correct at all. The generated HTML from your PHP in 1.php should closely follow the following format:


<form action="2.php" method="post">
<input type="hidden" name="item_ID" value="s001" />
<select name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="hidden" name="item_ID" value="s002" />
<select name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

If it doesn't look like that make your PHP output that in 1.php so that it is correct.

m3n9

9:45 am on Dec 24, 2008 (gmt 0)

10+ Year Member



<select name='<? echo $item_ID?>'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

<select name='<? echo $item_ID?>'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

If every item i have to choose two qty, how do i post it if i want the name='<? echo $item_ID?>' to be the same?