【问题标题】:Using ajax-jquery and fetch data using sql query.How to deal with response?使用 ajax-jquery 并使用 sql 查询获取数据。如何处理响应?
【发布时间】:2013-10-07 08:45:26
【问题描述】:

文件 1:test.php

$(document).ready(function() {        
 $.post( "test2.php")
 .done(function( data ) {
 alert(data);
 });
});

或 var var1="某事"; $(document).ready(function() {
$.post("test2.php",{demo:var1}) .done(函数(数据){ 警报(数据); }); });

文件2:test2.php

<?php $sql="SELECT * FROM xyz";
     $resultset=$database->query($sql);
     $found=$database->mysql_fetch_array($resultset);
     print_r($found); ?>

 `    $xyz=$_POST['demo'];
    <?php $sql="SELECT * FROM $xyz";
     $resultset=$database->query($sql);
     $found=$database->mysql_fetch_array($resultset);
     print_r($found); ?>`

所以,我的 test1.php alert(data); 将弹出带有一些 print_r 数组的警报 喜欢Array( [0]=>1 [name_id]=1 ) 类似的东西或任何在 test2.php 中的 html 代码,它将获取并显示

我的问题是 1)如何获取一些json或xml中的数据或一些方法来有效地解析这些数据? 2)如果我的 test2.php 包含许多查询,那么如何获取特定 ajax $.post 调用的特定数据?

请提出一些真实的答案!

【问题讨论】:

  • 问题1解决了!!
  • (2) 取决于您的数据库结构。请提供一个具体的例子。

标签: php jquery mysql ajax post


【解决方案1】:
use json_encode not print_r

<?php 
     $sql="SELECT * FROM xyz";
     $resultset=$database->query($sql);
     $found=$database->mysql_fetch_array($resultset);
     echo json_encode($found); 
?>

【讨论】:

    【解决方案2】:

    试试这个而不是print_r

    echo json_encode($found);
    

    然后,在 JS 中,您将必须执行一个 ajax 请求,并期待一个 JSON 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多