1

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?

Lorkhan
  • 71
  • 7

1 Answers1

-1

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

frogman578
  • 359
  • 1
  • 10