我有一个类似下面的表单发布到同一页面,用户可以使用jquery动态添加更多文本框。
<h1> Add Your Order </h1>
<form method="post" action="">
<p id="add_field"><a href="#"><span> Click to Add </span></a></p>
<div id="container">
<input type="text" class="pid" id="' + counter + '" name="Product[]" />
<input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
</div><br />
<input type= "submit" Name="submit_order" value="Submit">
</form>
一切正常但如果有人添加更多文本框并将一些文本框保留为空,那么它将提交。这是我的问题,我不想在我的表中提交空文本框,我想要服务器端解决方案。
这是我的完整代码,包含php
<body>
<?php
if ( isset($_POST['submit_order']) ) {
if ( !empty($_POST['Product']) && !empty($_POST['Quantity']) ) {
$product = ($_POST['Product']);
$quantity = ($_POST['Quantity']);
foreach ($product as $id => $value) {
$products = ($product[$id]);
$quantitys = ($quantity[$id]);
$query = mysql_query("INSERT iNTO myorders (product,quantity) VALUES ('$products','$quantitys')", $connection);
}
}
echo "<i><h2><stront>" . count($_POST['Product']) . "</strong> Entry Added </h2></i>";
mysql_close();
}
?>
<?php
if (!isset($_POST['submit_order'])) {
?>
<h1> Add Your Order </h1>
<form method="post" action="">
<p id="add_field"><a href="#"><span> Click to Add </span></a></p>
<div id="container">
<input type="text" class="pid" id="' + counter + '" name="Product[]" />
<input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
</div><br />
<input type= "submit" Name="submit_order" value="Submit">
</form>
<?php }
?>
</body>
答案 0 :(得分:2)
您可以使用array_filter
来获取非空数组的元素。如果非空元素的数量与原始数组大小不同,则用户将某些字段留空。
$filled_product = array_filter($product);
$filled_quantity = array_filter($quantity);
if (count($filled_product) < count($product) || count($filled_quantity) < count($quantity)) {
// Report error because of unfilled fields
}
答案 1 :(得分:0)
这样的事情应该有效:
foreach ($_POST[Product] as $key => $value):
if (empty($value)):
unset($_POST[Product][$key]);
endif;
endforeach;
foreach ($_POST[Quantity] as $key => $value):
if (empty($value)):
unset($_POST[Quantity][$key]);
endif;
endforeach;
答案 2 :(得分:0)
谢谢大家的帮助,遗憾的是没有人给我这个问题的完整解决方案 但@Wranorn给了我一个想法,我改变了我的代码,是的,这解决了我的问题
这是我的解决方案
<body>
<?php
if ( isset($_POST['submit_order']) ) {
$product = ($_POST['Product']);
$quantity = ($_POST['Quantity']);
foreach ($product as $id => $value) {
$products = ($product[$id]);
$quantitys = ($quantity[$id]);
if (!empty($products) && !empty($quantitys)) {
$query = mysql_query("INSERT iNTO myorders (product,quantity) VALUES ('$products','$quantitys')", $connection);
}
}
echo "<i><h2><stront>" . count($_POST['Product']) . "</strong> Entry Added </h2></i>";
mysql_close();
}
?>
<?php
if (!isset($_POST['submit_order'])) {
?>
<h1> Add Your Order </h1>
<form method="post" action="">
<p id="add_field"><a href="#"><span> Click to Add </span></a></p>
<div id="container">
<input type="text" class="pid" id="' + counter + '" name="Product[]" />
<input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
</div><br />
<input type= "submit" Name="submit_order" value="Submit">
</form>
<?php }
?>
</body>