dpg76975 2019-05-23 12:12
浏览 29

PHP选择框验证

I have a problem with select box validation in 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. 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. Or maybe the select box html is wrong. What would be the correct code?

  • 写回答

1条回答 默认 最新

  • doulan1073 2019-05-23 13:11
    关注

    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.

    Let me if there's anything more you'd like me to clarify :-)

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100