有没有办法将ajax数据作为单独的变量而不是完整的HTML获取?

时间:2012-11-13 16:15:22

标签: php jquery ajax

我正在尝试通过书籍的第一个字母jquery $.post将书行提取到我的<td> </td>中,这是我的表格的一部分。

我的表格如下:

<table id="myTable">

  <thead>
    <tr>
      <th class="page">pages</th>
      <th class="book_name">Book Name</th>
      <th class="author">Author</th>
    </tr>
  </thead>

  <tbody id="result_list">
  <!--Here is data which will come with ajax($.post) -->                 
  </tbody>
</table>

编写jquery代码,我成功了但是$("#result_list").html(data)像这样:

$.post( "/ajax.php", { book_first_letter: 'A'},
  function( data ) {
      $( "#result_list" ).html( data );
  }
);

但我不想将所有数据作为HTML获取并插入<tbody>。我想从ajax.php获取数据作为单独的变量。例如; BOOK_NAME,标志,页面,作者。我想将它们插入<td> </td>

我的ajax.php是这样的:

 $letter= trim($_POST['book_first_letter']);    
 $query = $this->book_model->get_books_by_letter($letter);       
 foreach ($query as $book) { ?>

     <tr>
         <td> <?php echo $book->page; ?> </td>
         <td> <?php echo $book->name; ?> </td>
         <td> <?php echo $book->author; ?> </td>

     </tr>
   <?php  
}
?>

有没有办法取每个变量(即$ book-&gt;名称)并将其插入<td></td>

6 个答案:

答案 0 :(得分:2)

您可以使用json_encode()

将数据转换为JSON
$letter= trim($_GET['book_first_letter']);    
$query = $this->book_model->get_books_by_letter($letter);    
echo json_encode( $query );

在客户端,然后使用getJSON()并在JavaScript中使用与PHP中类似的对象:

$.getJSON( "/ajax.php", { book_first_letter: 'A'},
  function( data ) {
      // data has the same properties as $query did in PHP
  }
);

修改

正如@MrCode getJSON()指出的那样,只需要一个GET请求。所以我改变了从$_POST$_GET访问PHP代码中参数的方式。

这里GET请求可能是更好的选择,因为这里的GET请求可以被浏览器缓存,而POST请求每次都必须执行。

另一个选择是保留POST请求,这会将客户端JS改为

$.post( "/ajax.php", { book_first_letter: 'A'},
  function( data ) {
      // data has the same properties as $query did in PHP
  }
  , 'json'
);

答案 1 :(得分:2)

不仅可以,而且你应该。 不是让PHP将html作为文本返回,而是让它返回一个JSON对象。

你需要做一些事情。 首先在$ .post()调用中添加第四个参数,数据类型为“json”。

$.post( "/ajax.php", { book_first_letter: 'A'}, callback, "json" );

然后,在PHP发送任何数据之前,让PHP为结果发送JSON头的好形式

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

而不是回显书籍模型的属性,只需使用json_encode()

$letter= trim($_POST['book_first_letter']);    
$books = $this->book_model->get_books_by_letter($letter);
echo json_encode( $books );

因此客户端现在拥有原始数据,您可以使用js / jQuery遍历书籍并将其添加到您的表中。

function callback( books)
{
   var i = 0, len = books.length, html = "";

    for( ; i < len; ; )
    {
        html += "however you want to structure the data";
    }

    $( "#result_list" ).html( html );
}

答案 2 :(得分:0)

JSON将是您完成此任务的最佳方式

根据你回到$query的内容,它应该像

一样简单
echo json_encode($query)

然后你必须在客户端javascript

处理数据

答案 3 :(得分:0)

是的,有 - 使用JSON。 PHP具有json_encode功能。只需使用json_encode从数据中创建一个类似json的数组,即可将它们发送到客户端。 在客户端,将$.postdataType: json一起使用。

答案 4 :(得分:0)

您可以将变量作为JSON返回。

在你的ajax.php中:

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);

在你的jquery AJAX调用中:

$.post('ajax.php', { book_first_letter: 'A'}, function(data) {
    console.log(data) //view the data returned in the console of firebug
},'json');

答案 5 :(得分:0)

Json编码。在我看来,很快你可能会开始考虑backbone & underscore堆栈以满足你的ajax需求。它在客户端上具有模板功能,看起来就像你在该示例中可以使用的东西之一