如何使用选择表单输出特定行?

时间:2018-07-26 21:50:34

标签: javascript php jquery forms

我有一个选择选项表格,我想从数据库中获取选择的数据。

该表如下所示:

id | brand | types | size
1 | nike | tshirt | L
2 | nike | shorts | XL
3 | puma | cap | foo
4 | jordans | shoes | foo

这是我尝试过的:

<?php
    include_once '../dbc.php';

    $brand = mysqli_real_escape_string($conn, $_POST['brand']);
    $type = mysqli_real_escape_string($conn, $_POST['type']);
    $size = mysqli_real_escape_string($conn, $_POST['size']);
    $price = mysqli_real_escape_string($conn, $_POST['price']);

    $sql = "SELECT * FROM cars WHERE brand='$brand'";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);

    if($resultCheck > 0){
        while($row = $result->fetch_assoc()) {
            echo '<option value="'.$row['type'].'">'.$row['type'].'</option>';
        }
    }
?>

结果应如下所示:选择nike时,应将数据库中的选项tshirtshorts添加到<select name="type">

<form id="clothesList">
<select name="brand">
    <option value="nike">nike</option>
    <option value="puma">puma</option>
    <option value="jordan">jordans</option>
</select>
<select name="type">
    <!-- Show here all avilable options from database -->
    <option value="tshirt">tshirt</option>
    <option value="shorts">shorts</option>
</select>
<select name="size">
    <!-- After selecting brand, type it should show the sizes -->
    <option value="L">L</option>
    <option value="M">M</option>
</select>
</form>

我还想出了更改后如何获取值:

$(document).ready( function() {

    $('#clothesList select').change(function(){
        console.log($(this).val());
    });

});

1 个答案:

答案 0 :(得分:0)

使用ajax拨打电话,假设您选择nike,使用ajax来获取nike下的选项,然后附加每个选项以进行选择。当您选择T恤时,请做同样的事情,直到没有更多的选择为止。