Reputation: 5510
I have several drop down lists in a table, I am trying to retrive the value of the drop down list if its not 0.
I have a table of items each item has the drop down list. Below is how I create the drop down list, I give it the tableid as a name because this is a unique identifier for the particular product.
echo "<td><select name=". $row['goosedown_id'] .">
<option value=''> 0 </option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
</select>
</td>";
Then once the form is submitted I am trying to just print any values i can... cause im not sure how to proceed... I do this like so.
$goosedown_id = check_input($_POST['goosedown_id']);
//..
goose down = $goosedown_id
I am hoping to be able to print out the associated values or calculate the price based off other items in the table i.e. price etc. but I am hoping someone can help me with trying to access the items that are no 0 first.
Upvotes: 0
Views: 4832
Reputation:
Have a look at the following example:
<!DOCTYPE html>
<head>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if (count($_POST)) {
for ($i = 0; $i < $_POST["nr_rows"]; $i++) {
echo "<h2>id=".$_POST["goosedown_id"][$i].", qty=".$_POST["qty"][$i]."</h2>\n";
}
die("<pre>".print_r($_POST, true)."</pre>\n");
} else {
// connect to the database
$link = mysqli_init();
// Adjust hostname, username, password and database name before use!
$db = mysqli_real_connect($link, "localhost", "root", "", "test") or die(mysqli_connect_error());
$SQL = "SELECT * FROM 5050goosedown ORDER BY price ASC";
$result = mysqli_query($link, $SQL) or die(mysqli_error($link));
$rows = array();
for ($i = 1; $row = mysqli_fetch_assoc($result); $i++){
$rows[] = array(
'id' => $row['goosedown_id'],
'bgcolor' => ($i % 2) ? '#F5F5F5' : '#E5E5E5',
'name' => htmlentities($row['name']),
'size' => ($row['width'] || $row['height']) ? $row['width'].'/'.$row['height'] : '',
'fill' => $row['normal_fill'].'/'.$row['our_fill'],
'old_price' => $row['old_price'] ? $row['old_price'] : '',
'price' => $row['price']
);
}
}
?>
<form action="<?= $_SERVER['SCRIPT_NAME'] ?>" method="post">
<table border="0" width="600">
<tr>
<th width="30%" colspan="2"><b>50/50 Goose Down:</b></th>
<th width="30%"><i>Normal Fill / Our Fill</i></th>
<th width="12.5%"><i>Old Price</i></th>
<th width="12.5%"><i>Price</i></th>
<th width="12.5%"><i>Quantity</i></th>
</tr>
<?php foreach ($rows as $row): ?>
<tr bgcolor="<?= $row['bgcolor'] ?>">
<td><?= $row['name'] ?></td>
<td><?= $row['size'] ?></td>
<td><?= $row['fill'] ?></td>
<td><?= $row['old_price'] ?></td>
<td><?= $row['price'] ?></td>
<td>
<select name="qty[]">
<?php for ($i = 0; $i <= 8; $i++): ?>
<option value="<?= $i ?>"><?= $i ?></option>
<?php endfor ?>
</select>
<input type="hidden" name="goosedown_id[]" value="<?= $row['id'] ?>" />
</td>
</tr>
<?php endforeach ?>
</table>
<input type="submit" name="myButton" id="myButton" value="Submit" />
<input type="hidden" name="nr_rows" value="<?= count($rows) ?>">
</form>
</body>
</html>
Upvotes: 1
Reputation: 360862
Did you look at the source of the page you're building? Your form is going to be
<td><select name="value_of_the_variable_your_insert">
not
<td><select name="goosedown_id">
since you're inserting $row['goosedown_id']
, you need to look for the VALUE of that variable, e.g.
$_POST[$row['goosedown_id']]
Upvotes: 1