[英]How to access all the <form> elements that have been dynamically generated by PHP-MySQL in $_GET upon submitting the form?
我有一个简单的数据库和一个小表。 此名为STOCK的表只有一列-文具中不同项目的名称。
我正在PHP-MySQL中动态生成一个表单,该表单从STOCK (例如Pencil,Pen等)中获取项目,并使用echo()
函数显示它们。 因此,我生成了一个显示Item并相应地显示<input>
文本框的表单。 提交此表单后,如何遍历$_GET
数组来访问所有名称及其文本输入?
动态生成的形式为:
$res = mysql_query("SELECT * FROM Stock");
echo "<form name='input' action='add_stock_later.php' method='get'>";
echo "<table align='left' border='1'>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Remarks</>
</tr>";
while($row = mysql_fetch_array($res))
{
$item = $row['Item'];
echo "<tr>";
echo "<td>" . $item . "</td>";
echo "<td><input type='text' name='$item'></td>";
echo "<td><input type='text' name='Remarks'></td>";
echo "</tr>";
}
echo "<tr><td></td>
<td align='middle'><input type='submit' value='Submit'></td></tr>";
echo "</table>";
现在在add_stock_later.php中 ,我如何访问所有这些$item
及其对应的输入,而不必使用$_GET['Pencil'], $_GET['Pen']
_ GET $_GET['Pencil'], $_GET['Pen']
等手动进行操作。
首先编辑您的表格。
echo "<td><input type='text' name='".$item."[quantity]'></td>";
echo "<td><input type='text' name='".$item."[remarks]'></td>";
然后使用foreach()
。
foreach ($_GET as $stationery => $info) {
$stationery; // name of the stationery
$info["quantity"]; // quantity of the stationery
$info["remarks"]; // remarks for the stationery
}
就像这样,当然要加上它。
$required = array('pencil', 'pen', 'paper', 'staples');
foreach ($required as $getKey){
if (isset($_GET[$getKey]) && $_GET[$getKey] !=''){
$$getKey = trim($_GET[$getKey]);
}
}
这会将$_GET['pencil']
变成$pencil
而您不必担心试图注入垃圾的指关节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.