php - PHP选择框验证
问题描述
我在 PHP 中的选择框验证有问题。基本上我需要做的是,如果用户从选择框中选择 DVD-Disc,如果他将 Size 字段留空,则代码应该阻止他发布表单。如果他选择 Book,应该提示他输入 Weight。家具也是一样。
表格如下所示:
<form action="" method="post">
<div class="form-group">
<label for="sku">SKU</label> <span class="errorMessage"><?php echo $errorSKU; ?></span>
<input type="text" class="form-control" id="sku" name="sku" value="<?php echo $sku; ?>">
</div>
<div class="form-group">
<label for="name">Name</label> <span class="errorMessage"><?php echo $errorName; ?></span>
<input type="text" class="form-control" id="name" name="name" value="<?php echo $name; ?>">
</div>
<div class="form-group">
<label for="price">Price</label> <span class="errorMessage"><?php echo $errorPrice; ?></span>
<input type="text" class="form-control" id="price" name="price" value="<?php echo $price; ?>">
</div>
<div class="form-group">
<label for="type">Type</label> <span class="errorMessage"><?php echo $errorType; ?></span>
<select id="type" name="type" class="form-control">
<option></option>
<option value="attributes_size">DVD-disc</option>
<option value="attributes_weight">Book</option>
<option value="attributes_dimensions">Furniture</option>
</select>
</div>
<div id="attributes_size" class="attributes_div" style="display:none;">
<label for="">Attributes</label>
<div class="form-group" style="padding-left: 30px;">
<label for="size">Size</label>
<input type="text" class="form-control" id="size" name="size">
</div>
</div>
<div id="attributes_weight" class="attributes_div" style="display:none;">
<label for="">Attributes</label>
<div class="form-group" style="padding-left: 30px;">
<label for="weight">Weight</label>
<input type="text" class="form-control" id="weight" name="weight">
</div>
</div>
<div id="attributes_dimensions" class="attributes_div" style="display:none;">
<label for="">Attributes</label>
<div class="form-group" style="padding-left: 30px;">
<label for="dimensions[height]">Height</label>
<input type="text" class="form-control" id="height" name="dimensions[height]">
</div>
<div class="form-group" style="padding-left: 30px;">
<label for="dimensions[width]">Width</label>
<input type="text" class="form-control" id="width" name="dimensions[width]">
</div>
<div class="form-group" style="padding-left: 30px;">
<label for="dimensions[length]">Length</label>
<input type="text" class="form-control" id="length" name="dimensions[length]">
</div>
</div>
<input type="submit" name="submit" id="submit" class="btn btn-default">
</form>
以及验证代码:
require_once "classes/dbconn.php";
require_once "classes/products.php";
$sku = "";
$name = "";
$price = "";
$type = "";
$errorSKU = "";
$errorName = "";
$errorPrice = "";
$errorType = "";
$errorCount = 0;
if(isset($_POST['submit'])) {
if(!empty($_POST['sku'])) {
$sku = $_POST['sku'];
}
else {
$errorSKU = "SKU is required!";
$errorCount ++;
}
if(!empty($_POST['name'])) {
$name = $_POST['name'];
}
else {
$errorName = "Name is required!";
$errorCount ++;
}
if(!empty($_POST['price'])) {
$price = $_POST['price'];
}
else {
$errorPrice = "Price is required!";
$errorCount ++;
}
if(!empty($_POST['type'])) {
$type = $_POST['type'];
}
else {
$errorType = "Type is required!";
$errorCount ++;
}
$attributes = '';
if($type = "DVD-disc") {
if(isset($_POST['size']) && !empty($_POST['size'])) {
$attributes = "Size: " . $_POST['size'] . " Mb";
}
else {
$errorType = "Type and attributes are required! Disc";
$errorCount ++;
}
}
if($type = "Book") {
if(isset($_POST['weight']) && !empty($_POST['weight'])) {
$attributes = "Weight: " . $_POST['weight'] . " kg";
}
else {
$errorType = "Type and attributes are required! Book";
$errorCount ++;
}
}
if($errorCount > 0) {
// echo $errorCount;
}
else {
$fields = [
'SKU'=>$sku,
'Name'=>$name,
'Price'=>$price,
'Type'=>$type,
'Attributes'=>$attributes
];
$product = new Products();
$product->insert($fields);
}
}
问题是当我选择光盘并输入尺寸时,它一直在询问我的书本和重量字段。反之亦然。问题显然出在 if 语句中。或者也许选择框 html 是错误的。什么是正确的代码?
解决方案
您的后端 PHP 可能无法识别空<option>
并引发某种错误,请尝试将其删除和/或尝试在按钮$_POST['submit']
子句中发布您的输入。
如果您还有什么需要我澄清的,请告诉我:-)
推荐阅读
- c# - C#如何将默认实现添加到覆盖另一个接口的接口
- reactjs - 世博不再工作?bash:世博会:找不到命令
- swift - 如何构造值以使所有视图都可以在 Swift/SwiftUI 中访问其值?
- javascript - 尝试使用提示时,addEventListener 在页面加载时触发
- node.js - 无法将异步操作结果保存在数组中
- julia - 如何在 Julia 中使用嵌套列表推导
- android - Jetpack Compose LazyColumn 滚动监听器
- java - java.sql.drivermanager 不接受使用公共 IP 的 mysql jdbc url,没有合适的驱动程序
- node.js - Node.JS + Puppeteer:browser.close 冻结进程
- python - 将 mapbox choropleth 与 python 中的附加层和标记相结合;尝试覆盖坐标标记