我在表中有几个下拉列表,如果它不是0,我试图检索下拉列表的值。
我有一个项目表,每个项目都有下拉列表。下面是我创建下拉列表的方法,我将tableid作为名称,因为这是特定产品的唯一标识符。
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>";
然后,一旦表单提交,我试图打印任何值,我可以...因为我不知道如何继续......我这样做。
$goosedown_id = check_input($_POST['goosedown_id']);
//..
goose down = $goosedown_id
我希望能够打印出相关值或根据表格中的其他项目计算价格,即价格等。但我希望有人可以帮助我尝试访问不是0的项目。
答案 0 :(得分:1)
你看过你正在建造的页面的来源吗?你的表格将是
<td><select name="value_of_the_variable_your_insert">
不
<td><select name="goosedown_id">
因为您要插入$row['goosedown_id']
,所以需要查找该变量的VALUE,例如
$_POST[$row['goosedown_id']]
答案 1 :(得分:1)
请看下面的例子:
<!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>