如何使用mysqli,jquery ajax从php获取两个数组?

时间:2017-10-23 04:43:57

标签: php jquery ajax mysqli

我希望函数(数据)从数据库中获取2个属性值

这是选择选项,用于在输入字段中显示所选选项的地址和联系人值

 $('#recipient').change(function(){  
        var FULL_NAME = $(this).val();  
        $.ajax({  
            url:"load_data.php",  
            method:"POST",  
            data:{FULL_NAME:FULL_NAME},  
            success:function(data){  
                     $('#address').val(data);
                     $('#contact').val(data);  
            }  
        });

    });

这是load_data.php

<?php
      $sql = "SELECT * FROM recipient";
      $result = mysqli_query($connect, $sql);  
      while($row = mysqli_fetch_array($result))  
      {
        $output1 = $row["ADDRESS"];
        $output2 = $row["CONTACT"];
        $arr = array($output1,$output2);  
      }  
      echo $output1,$output2;
?>

如何将$ output1传递给$(&#39; #address&#39;)。val(数据)和$ output2到$(&#39; #contact&#39;)。val(数据)< / p>

1 个答案:

答案 0 :(得分:-1)

$('#recipient').change(function(){  
        var FULL_NAME = $(this).val();  
        $.ajax({  
            url:"load_data.php",  
            method:"POST",
            dataType: "json",
            data:{FULL_NAME:FULL_NAME},  
            success:function(data){  
                     $('#address').val(data['ADDRESS']);
                     $('#contact').val(data['CONTACT']);  
            }  
        });

});

和load_data.php

<?php
      $result = mysqli_query($connect, $sql);  
      while($row = mysqli_fetch_array($result))  
      {
        $arr["ADDRESS"] = $row["ADDRESS"];
        $arr["CONTACT"] = $row["CONTACT"]; 
      }  
     echo json_encode($arr);
?>