【问题标题】:store JSON values in input fields在输入字段中存储 JSON 值
【发布时间】:2017-08-31 23:34:12
【问题描述】:

你好,我正在寻求帮助

我正在学习如何使用 Ajax 和 PHP,我想做的是在 PHP 中运行查询并将结果存储在 JSON 中。
然后我想echo JSON 并将它的值设置到文本字段中。

这可能吗?

由于我对 Ajax 和 jQuery 还很陌生,我不知道该怎么做。
我试图这样做,但我只得到数组的第一个值。

这是我的代码:

    <input type="text" id="text1">
    <button type="button" class="btn btn-success" id="send">Success Button</button>
    <input type="text" id="text2">
    <input type="text" id="text3">
    <input type="text" id="text4">


  <script type="text/javascript">
  $(document).ready(function(){
    $("#send").click(function(event){
      event.preventDefault();
      var Rfc=$("#text1").val();

      $.ajax({
        type: 'POST',
        url: 'search.php',
        data: 'Rfc='+Rfc,
        dataType : 'json',
        success: function(msg){
          var datashow = JSON.parse(msg);
          $("#text2").val(msg[0].id_person); ///this is the only value that i get
          $("#text3").val([1].person_name); ///i want to get the person's name here
          $("#text4").val([2].person_address);///i want to get the person's address here
        },
        error : function () {
          alert("error");
        }
      });
    });
  });
  </script>

这是我的 PHP 文件:

<?php 

$rfc=$_POST['Rfc'];

$connection = mysqli_connect("localhost","root","","transferorders");
$query = mysqli_query($connection,"SELECT * FROM person where rfc_number ='$rfc'");
$Data = array();

while($row = mysqli_fetch_assoc($query)){
  $Data[]=$row;
  echo json_encode($Data);   
}

?>

这是我在控制台中得到的

未捕获的类型错误:无法读取未定义的属性“person_name” 在 Object.success(测试 ajax1.php:40)

【问题讨论】:

  • 首先,我建议您正确缩进您的代码,这将有助于您和其他审查您的代码的人的可读性。您可能还想检查您的代码并确保所有标签都已关闭(例如,&lt;/html&gt; 缺失)。还有一些 &lt;script&gt; 标签应该放在 &lt;head&gt;&lt;body&gt; 部分中,但它们却位于不知名的地方。
  • msg[0].person_name ?
  • 我会先看看一些基本的 HTML 标记问题...在尝试弄乱脚本循环之前。
  • 1) &lt;script&gt; 标签必须在 &lt;head&gt;&lt;body&gt; 内。 2) 您是否正在加载两个引导程序版本? 3)你应该echo返回数组之后 PHP循环结束了。
  • Console.log 在成功回调中使用console.log(JSON.stringify(msg)); 记录您的结果...只是看看您得到了什么结果。

标签: php jquery html mysql ajax


【解决方案1】:

php 文件中的兄弟尝试识别数组中的每个变量以在脚本中捕获它们,看看这个:

<?php 

$rfc=$_POST['Rfc'];

$connection = mysqli_connect("localhost","root","","transferorders");
$query = mysqli_query($connection,"SELECT * FROM person where rfc_number ='$rfc'");

$row = mysqli_fetch_array($query)

    $Data = '{"id":"'.$row['id_person'].'", "name":"'.$row['person_name'].'", "address":"'.$row['person_address'].'"}';

    echo json_encode($Data);

?>

和脚本:

success: function(msg){
          var datashow = JSON.parse(msg);
          $("#text2").val(datashow["id"]); ///this is the only value that i get
          $("#text3").val(datashow["name"]); ///i want to get the person's name here
          $("#text4").val(datashow["address"]);///i want to get the person's address here
        },

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 2017-06-06
    • 2012-01-27
    • 1970-01-01
    • 2021-05-20
    • 2021-10-05
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多