douba9776 2015-08-20 14:36
浏览 56
已采纳

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

I have a selectbox with options generated with data from a database(phpmyadmin) the database looks like this:

Columns: locationID - address - postalcode - place

After the select box I have some input fields, at first standard with some placeholders.

The idea is that if I select an option that the fields will be generated and filled from the database (except the contactperson field, this will stay manual)..

For this I try to use AJAX.

I have already made this:

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'] ?>" /> 
} 
?>

unfortunatly, it doesn't work.

  • 写回答

2条回答 默认 最新

  • doucheng4094 2015-08-20 14:49
    关注

    try this

    echo json_encode(mysqli_fetch_assoc($result));
    

    in your script loop, console log data to view the result for troubleshooting
    change your script to

    $(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);
          }
        }
      });
    });
    

    check your console log for errors

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

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA