dso407787736 2019-05-20 17:58
浏览 67
已采纳

根据PHP中的用户选择发布表单值

I need to make a form where users can insert products in a database. The database has columns "Name", "SKU", "Type", "Attributes". The form has fields "Name", "SKU", "Price" and a select box "Type" with three options - "Furniture", "Dvd-disc" and "Book".

Additional form fields are show based on what user chooses in the "Type" selection. For example, if the user chooses "Furniture" from the select box, then three fields, "Height", "Width", "Length", appear. If he chooses "Books", a "Weight" field appears, and if he chooses "DVD-disc", the field "Size" appears.

The jQuery funcion to hide/show required fields is not implemented yet, so don't mind that.

EDIT: When I post, for example, the type Disc and Size 700, and do "var_dump($_POST);" I get:

array(8) { ["sku"]=> string(8) "BT944RUR" ["name"]=> string(22) "Third insert from page" ["price"]=> string(5) "56.25" ["type"]=> string(8) "DVD-disc" ["size"]=> string(3) "700" ["dimensions"]=> array(3) { ["height"]=> string(0) "" ["width"]=> string(0) "" ["length"]=> string(0) "" } ["weight"]=> string(0) "" ["submit"]=> string(6) "Submit" }

It collects the Size value from the form but doesn't pass it further.

HTML form:

<form action="" method="post">
    <div class="form-group">
        <label for="sku">SKU</label>
        <input type="text" class="form-control" id="sku" name="sku">
    </div>
    <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name" name="name">
    </div>
    <div class="form-group">
        <label for="price">Price</label>
        <input type="text" class="form-control" id="price" name="price">
    </div>
    <div class="form-group">
        <label for="type">Disabled select menu</label>
        <select id="type" name="type" class="form-control">
            <option></option>
            <option>DVD-disc</option>
            <option>Book</option>
            <option>Furniture</option>
        </select>
    </div>
    <div class="form-group">
        <label for="size">Size</label>
        <input type="text" class="form-control" id="size" name="size">
    </div>
    <div class="form-group">
        <label for="dimensions[height]">Height</label>
        <input type="text" class="form-control" id="height" name="dimensions[height]">
    </div>
    <div class="form-group">
        <label for="dimensions[width]">Width</label>
        <input type="text" class="form-control" id="width" name="dimensions[width]">
    </div>
    <div class="form-group">
        <label for="dimensions[length]">Length</label>
        <input type="text" class="form-control" id="length" name="dimensions[length]">
    </div>
    <div class="form-group">
        <label for="weight">Weight</label>
        <input type="text" class="form-control" id="weight" name="weight">
    </div>
    <input type="submit" name="submit" id="submit" class="btn btn-default">
</form>

PHP code so far (to pass the variables to the insert):

if(isset($_POST['submit'])) {
    $sku = $_POST['sku'];
    $name = $_POST['name'];
    $price = $_POST['price'];
    $type = $_POST['type'];
    $attributes = '';

    if(isset($_POST['dimensions']) && !empty($_POST['dimensions'])) {
        $attributes = implode(' x ', $_POST['dimensions']);
    }
    else if(isset($_POST['weight']) && !empty($_POST['weight'])) {
        $attributes = $_POST['weight'];
    }
    else if(isset($_POST['length']) && !empty($_POST['length'])) {
        $attributes = $_POST['length'];
    }

    $fields = [
        'SKU'=>$sku,
        'Name'=>$name,
        'Price'=>$price,
        'Type'=>$type,
        'Attributes'=>$attributes
    ];

    $product = new Products();
    $product->insert($fields);
}

Insert function (in a different file):

    public function insert($fields) {

        $implodeColumns = implode(', ', array_keys($fields));
        $implodeValues = implode(", :", array_keys($fields));

        $sql = "INSERT INTO products ($implodeColumns) VALUES (:".$implodeValues.")";

        $result = $this->connect()->prepare($sql);

        foreach ($fields as $key => $value) {
            $result->bindValue(':'.$key, $value);
        }

        $resultExec = $result->execute();

        if($resultExec) {
            header('Location: index.php');
        }
    }

I think the if statements are wrong. I can pass the dimensions to the database in such a way but if I try to send size or weight it just passes epmty values with two "x" between them.

And additional question: can I achieve this without using conditional statements? I'm open to different approaches.

  • 写回答

2条回答 默认 最新

  • doushuangdui5419 2019-05-21 06:26
    关注

    The problem is your first if:

    if(isset($_POST['dimensions']) && !empty($_POST['dimensions'])) {
    

    As seen in your HTML, dimension is an array of inputs: length, height, width. This means the variable will never be empty.

    $dimensions = [
        'length' => '',
        'width'  => '',
        'heigth' => '',
    ];
    
    echo empty($dimensions) ? 'dimensions is empty': 'dimensions is not empty'; //output: dimensions is not empty
    

    As solution would to "clean" out nullable values from your array before testing if its empty. To do this, you can resort to array_filter

    echo empty(array_filter($dimensions)) ? 'dimensions is empty': 'dimensions is not empty'; //output: dimensions is empty
    

    demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。