在PHP中更改数据库中的下拉值

时间:2015-10-14 05:53:28

标签: javascript php

form

     <select name="state_hid" id="first-choice" required     ><option>AL</option><option>AK</option><option>AZ</option><option>AR</option>
         <option>CA</option>
        <option>CO</option>
         <option>CT</option><option>DE</option><option>FL</option>         <option>GA</option>
        <option>HI</option><option>ID</option>
         <option>MN</option>

</select> 

database  我想根据状态下拉列表中的值来获取值。

请帮忙

1 个答案:

答案 0 :(得分:1)

Html代码:

<?php
    $sql = "SELECT * FROM country ORDER BY country";
    $query = mysqli_query($con, $sql);
?>
<td>
    <lable>Country:</lable>
</td>
<td>
    <select name="country" id="country" class="country">
        <option selected="selected">--Select Country--</option>
        <?php while ($row1 = mysqli_fetch_array($query)) { ?>
            <option value="<?php echo $row1["c_id"]; ?>">
                <?php echo $row1["country"]; ?>
            </option>
        <?php } ?>
    </select>
<td>
    <lable>State:</lable>
</td>
<td>
    <select name="state" id="state" class="state" selected="selected">
        <option value="select">--Select State--</option>
    </select>
</td>

这是州政府的ajax

$(document).ready(function () {
  $(".country").change(function () {
    var id = $(this).val();
    var dataString = 'id=' + id;

    $.ajax({
      type: "POST",
      url: "state.php",
      data: dataString,
      cache: false,
      success: function (html) {
        alert(id);
        $(".state").html(html);
      }
    });
  });
});

用于geting状态的PHP代码:

<?php
    include('config.php');
    if ($_POST['id']) {
        $id = $_POST['id'];
        $sql = "SELECT s_id, state_name FROM state WHERE c_id='$id'";
        $result = mysqli_query($con, $sql);
        while ($row = mysqli_fetch_array($result)) {
            $id = $row['s_id'];
            $data = $row['state_name'];
            echo '<option value="' . $id . '"   ' . $country_status . ' >' . $data . '</option>';
        }
    }
?>