使用AJAX、PHP和Javascript自动填充输入文本框


auto fill input text boxes using AJAX and PHP and Javascript

我有一个选择框与从数据库(phpmyadmin)的数据生成的选项,数据库看起来像这样:

Columns: locationID - address - postalcode - place

在选择框之后,我有一些输入字段,首先是一些标准的占位符。

这个想法是,如果我选择一个选项,字段将从数据库生成和填充(除了contactperson字段,这将保持手动)..

对于这个,我尝试使用AJAX。

我已经写好了:

test.php :

<?php
    //Query the database for the results we want
    $query1 = $conn->query("SELECT * FROM deliveryaddress");
    //Create an array of objects for each returned row
    while($locationArray[] = $query1->fetch_object());
    //Remove the blank entry at end of array
    array_pop($locationArray);
?>
<script>
    $(document).on("change","#select-box").function(){
        var id = $(this).val();
        $.ajax({
            url      : "locationAjax.php",
            data     : {"id":id},
            type     : "POST",
            dataType : "HTML",
            success  : function(data){
                // here you can write code to asign the values to text boxes.
                $(".wrapperDiv").html(data); 
            }
        });
    });
</script>
<div class="wrapperDiv">
    <label for="locationLabel">Locatie</label>
    <select name="locations" id="locationSelectBox" >
        <option>Locatie</option>
        <?php foreach ($locationArray as $option) : ?>
            <option value="<?php echo $option->locationID; ?>"><?php echo $option->locationID; ?></option>
        <?php endforeach; ?>
    </select>
    <label for="address" style="float:left;">Straat/No</label>
    <input type="text" name="address" id="address" placeholder="Straatnaam en nummer" />
    <label for="postalCode">Postcode</label>
    <input type="text" name="postalCode" id="postalCode" placeholder="Postcode" />
    <label for="place">Plaats</label>
    <input type="text" name="place" id="place" placeholder="Plaats" />
    <label for="contactPerson">Contact</label>
    <input type="text" name="contactPerson" id="contactPerson" placeholder="Contactpersoon" />
</div>

locationAjax.php :

<?php
    require_once('dbconnection.php'); 
    $locationID = $_POST['id']; 
    $sql = "SELECT * FROM deliveryaddress where id = $locationID"; 
    $result = mysqli_query($conn, $sql); 
?> 
<?php while ($row = mysqli_fetch_assoc($result)) { ?> 
    <input type="text" name="name" value="<?= $row['address'] ?>" /> 
    <input type="text" name="name" value="<?= $row['postalcode'] ?>" /> 
    <input type="text" name="name" value="<?= $row['place'] ?>" /> 
} 
?>

try this

echo json_encode(mysqli_fetch_assoc($result));

在脚本循环中,控制台日志数据以查看故障排除的结果
将脚本更改为

$(document).on("change", "#locationSelectBox").function() {
  var id = $(this).val();
  $.ajax({
    url : "locationAjax.php",
    data : {
     "id" : id
    },
    type : "POST",
    dataType : "json",
    success : function(data) {
      console.log(data);
      //
      for (var x in data) {
        $('.address').val(data.address);
        $('.postalcode').val(data.postalcode);
        $('.place').val(data.place);
      }
    }
  });
});

检查控制台日志是否有错误

代替这一行:$(文档)。("改变"、"#"选择框).function () {

试试这个:$(文档)。("改变"、"# locationSelectBox").function () {

然后console.log ajax "success"函数的数据告诉我你得到了什么