【问题标题】:How to get two arrays from php using mysqli,jquery ajax?如何使用mysqli,jquery ajax从php获取两个数组?
【发布时间】:2017-10-23 04:43:56
【问题描述】:

我希望函数(数据)从数据库中获取 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 传递给 $('#address').val(data) 并将 $output2 传递给 $('#contact').val(data)

【问题讨论】:

  • 使用 json_encode 并输出 arr。在 js data[0]
  • 用分隔符连接两个字符串,在 ajax 中使用 split() 分别获取两个值。

标签: php jquery ajax mysqli


【解决方案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);
?>

【讨论】:

    猜你喜欢
    • 2013-03-18
    • 2020-10-06
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多