Retrieving Post Values From Form With Dynamic Element Names
Solution 1:
Firstly I would change
name="s_<?php echo $colour_row[colour_name]; ?>"
etc to
name="attributes[s_<?php echo $colour_row[colour_name]; ?>]"
And use the following PHP
if( !empty($_POST['attributes']) ) {
foreach( $_POST['attributes'] as $sKey => $iQty ) {
var_dump( $sKey );
var_dump( $iQty );
}
} else {
die( 'Just for debuging. attributes-array was empty' );
}
Or even better
use
name="attributes[xxl][color]" eg. name="attributes[xxl][<?php echo $colour_row[colour_name]; ?>]"
And
if( !empty($_POST['attributes']) ) {
foreach( $_POST['attributes'] as $sSize => $aData ) {
var_dump( $sSize );
var_dump( $aData );
}
}
Solution 2:
Ok i now have it displaying as a list on the second page using the following code :
<td><center><?php if($colour_row['xxl'] !== '') { echo('<input type="text" size="2" name="xxl_' . $colour_row['colour_name'] . '" placeholder="Qty">');
} else { echo(''); } ?> </center></td>
and then :
<?php
$product_code=$_POST["product_code"];
foreach($_POST as $key=>$value)
{
echo "$key=$value";
echo "<br />";
}
?>
on the second page.
thank you all for your help, your suggestions put me on the right track, much appreciated.
Solution 3:
You put PHP code inside a string, it does not get parsed by PHP that way:
echo '<input name="xs_<?php echo $colour_row[colour_name]; ?>">';
This should probably be concatenated like so:
echo '<input name="xs_' . $colour_row[colour_name] . '">';
The same goes for
echo '<input id="xs_<?php echo $row[colour_name]; ?>">';
Which needs to be converted to:
echo '<input id="xs_' . $row[colour_name] . '">';
Also is it possible that "$row" has to be "$colour_row" in the id attribute of the input field?
There are some other obvious improvements to be made, but those are outside the scope of your question.
I was closing my Netbeans and found this still sitting there, I hope it's of use ( not 100% guaranteed to work out-of-the-box ):
The form php would become:
<?php
$recordID = $_GET["recordID"];
// I adjusted the query somewhat.
// The colours tabel is joined with a USING() command
// The recordId is escaped
$colour_result = mysqli_query($con, "
SELECT colour_variation.*, colours.*
FROM colour_variation INNER JOIN colours USING(colour_id)
WHERE product_code = '". mysqli_escape_string($con, $recordID)."'
");
// I'm using an alternative syntax for the while control structure ( I am
// using a colon instead of braces ):
while ($colour_row = mysqli_fetch_array($colour_result)): ?>
<tr>
<td valign="middle"><img src="resources/images/colours/<?php echo $colour_row['colour_image']; ?>" width="35" height="35"></td>
<td width="100"><?php echo $colour_row['colour_name']; ?></td>
<!-- I'm using an alternative syntax for the if control structures here -->
<!-- I also left out the else, since it did not print anything -->
<!-- I used an array for the name notation, to easily read out the data -->
<td><center><?php if ($colour_row['xs'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xs]" placeholder="Qty"><?php endif; ?> </center></td>
<td><center><?php if ($colour_row['s'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][s]" placeholder="Qty"><?php endif; ?> </center></td>
<td><center><?php if ($colour_row['m'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][m]" placeholder="Qty"><?php endif; ?> </center></td>
<td><center><?php if ($colour_row['l'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][l]" placeholder="Qty"><?php endif; ?> </center></td>
<td><center><?php if ($colour_row['xl'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xl]" placeholder="Qty"><?php endif; ?> </center></td>
<td><center><?php if ($colour_row['xxl'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xxl]" placeholder="Qty"><?php endif; ?> </center></td>
<td><center><?php if ($colour_row['xxxl'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xxxl]" placeholder="Qty"><?php endif; ?> </center></td>
<td><center><?php if ($colour_row['one_size'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][one_size]" placeholder="Qty"><?php endif; ?> </center></td>
</tr>
<?php endwhile; ?>
</table>
<input type="hidden" name="product_code" value="<?php echo $colour_row_products['product_code']; ?>" >
The receiving end would be something like this:
<?php
$iProductCode = $_POST['product_code'];
foreach($_POST['quantity'] as $sColor => $aColourQuantityData) {
foreach($aColourQuantityData as $sSize => $iQuantity) {
echo "Product #$iProductCode, colour $sColor, size $sSize: $iQuantity pieces<br>";
}
}
?>
Post a Comment for "Retrieving Post Values From Form With Dynamic Element Names"