PHP选择框验证

时间:2019-05-23 12:12:01

标签: php html

我在PHP中使用选择框验证存在问题。基本上,我需要做的是,如果用户从选择框中选择DVD-Disc,那么如果他将“大小”字段留为空白,则代码应阻止他发布表单。如果选择“书本”,则应提示他输入“体重”。家具也一样。

表单如下:

<form action="" method="post">
    <div class="form-group">
        <label for="sku">SKU</label>&nbsp;<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>&nbsp;<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>&nbsp;<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>&nbsp;<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是错误的。什么是正确的代码?

1 个答案:

答案 0 :(得分:-1)

您的后端PHP可能无法识别空的<option>并引发某种错误,请尝试将其删除和/或尝试将输入内容发布在按钮$_POST['submit']子句中。

还有什么想让我澄清的:-)