Page 1 of 1

[SOLVED] error passing variable through from <select>

Posted: Mon Sep 11, 2006 8:09 am
by rsmarsha
I'm passing variables through from a from such as :

Code: Select all

<td align="center"><input type="text" name="<?php echo 'amount['.$srow['product_id'].']'; ?>" id="amount" size="3"></td>
<td align="center">£<input type="text" name="<?php echo 'buy_price['.$srow['product_id'].']'; ?>" id="buy_price" value="<?php echo ''.$srow['buy_price'].''; ?>" size="3"></td>
<td align="center"><input type="checkbox" name="<?php echo 'order['.$srow['product_id'].']'; ?>" id="order"></td>
<select name="<?php echo 'supplier['.$srow['product_id'].']'; ?>" id="supplier">
<?php if (isset($_POST['suppliers']))
{
$s2 = "SELECT * FROM suppliers WHERE id='".$_POST['suppliers']."'";
$sq2 = mysql_query($s2) or die ("Query $s2 Failed".mysql_error());
$sr2 = mysql_fetch_assoc($sq2);
	?>
	<option value="<?php echo $_POST['suppliers']; ?>"><?php echo $sr2['name']; ?></option>
	<?
}
	$s = "SELECT * FROM suppliers";
$sq = mysql_query($s) or die ("Query $s Failed".mysql_error());
while ($sr = mysql_fetch_assoc($sq))
{
	?>
	<option value="<?php echo $sr['id']; ?>"><?php echo $sr['name']; ?></option>
	<?php
}
	?>
	</select>
They are then used (atm for testing), in the form:

Code: Select all

foreach($_POST['order'] as $orderID=>$orderValue)
{

	$amount = current($_POST['amount']);
	next($_POST['amount']);
	$buy_price = current($_POST['buy_price']);
	next($_POST['buy_price']);
	$supplier = current($_POST['supplier']);
	next($_POST['supplier']);
	echo 'S='.$supplier.',A='.$amount.',P='.$buy_price.'<br />';
}
Which works fine for all but the select list. I can't get that to post through and work as the others in the above loop. Any ideas what i'm doing wrong?

Posted: Mon Sep 11, 2006 8:46 am
by GM
I think the problem is here:

Code: Select all

<select name="<?php echo 'supplier['.$srow['product_id'].']'; ?>" id="supplier">
I think you are passing as a name for the select field something like "supplier[12345]".

Have a check of the html source of your page, to find how the name of your select field is actually rendered by php.

Posted: Tue Sep 12, 2006 2:29 am
by rsmarsha
Yes it is, but i want it to do that, so it knows which product in the list that supplier is for. Also to make it post them into an array so i can work through them like the one above. For some reason i can't work out though it's not posting them as the others.

Posted: Tue Sep 12, 2006 2:35 am
by rsmarsha
It seems to have started working for some strange reason. :) So all is well.