如何根据用户填写的html字段填写html选项中的选项。

时间:2014-04-24 06:11:20

标签: php mysqli

以下是代码段:

<select name="isbn" onchange=" ">

<?php

//Displaying all ISBN in drop down
$result = mysqli_query($con,"SELECT * FROM book");
while($row = mysqli_fetch_array($result)) {
?>
    <option> <?php echo $row['ISBN']; ?></option>
<?php } ?>
</select>
</label></td>
</tr>

<tr>
    <td>Copy Number </td>
    <td><select name="copy_number">
<?php

//Now based on selected book I want to fetch number of copies from database
$result = mysqli_query($con,"SELECT number_of_copies FROM book where isbn = [ SELECTE VALUE FROM ABOVE]");
?>

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery ajax和简单的php处理程序来获取所选书籍的副本

<select name="isbn">
.....
</select>

<div id="copies"></div>

<script>

$.ajax({
        url: "getCopies.php?",
        data: "isbn=" + $("select[name='isbn']").val(),
        type: "POST",
        dataType: "json",
        success: function(response) {
            $.each(response, function(i, item) {
                $("#copies").append(item.name); // Sample json format {id: "213123", name:"Lord of the rings", isbn:"887799..."}
            })
        }   

    });

</script>

<强> getCopies.php

<?php

$isbn = $_POST["isbn"];

// Some db connections

$result = mysqli_query($con,"SELECT number_of_copies FROM book where isbn = $isbn");

$resultArr = array();

while($row = mysqli_fetch_array($result)) {
    $resultArr[] = $row;
}

echo json_encode($resultArr); // This will return rows in json format. You can iterate it in js side