简体   繁体   English

PHP选择框验证

[英]PHP select box validation

I have a problem with select box validation in PHP. 我在PHP中使用选择框验证存在问题。 Basically what I need to do is if a user chooses DVD-Disc from the select box the code should prevent him from posting the form if he left the Size field empty. 基本上,我需要做的是,如果用户从选择框中选择DVD-Disc,那么如果他将“大小”字段留为空白,则代码应阻止他发布表单。 If he chooses Book, he should be prompted to input Weight. 如果选择“书本”,则应提示他输入“体重”。 The same with Furniture. 家具也一样。

The form looks like this: 该表格如下所示:

<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>

And the code for validation: 以及验证代码:

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);
    }

}

The problem is when I choose Disc and enter Size it keeps asking me for the fields Book and Weight. 问题是,当我选择“光盘”并输入“尺寸”时,它会不断询问我“书本”和“重量”字段。 And vice versa. 反之亦然。 The problem obviously is in the if statements. 问题显然在if语句中。 Or maybe the select box html is wrong. 或选择框html是错误的。 What would be the correct code? 正确的代码是什么?

It's possible your backend PHP doesn't recognise the empty <option> and throws some kind of error, try removing it and/or try posting your inputs within the button $_POST['submit'] clause. 您的后端PHP可能无法识别空的<option>并引发某种错误,请尝试将其删除和/或尝试将输入内容发布在按钮$_POST['submit']子句中。

Let me if there's anything more you'd like me to clarify :-) 如果还有什么要让我澄清的,请让我:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM