有没有办法将ajax数据作为单独的变量而不是完整的HTML获取?(Is there any way to fetch ajax data as separate variable instead of full HTML?)

我试图通过书籍的第一个字母和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-> name)并将其插入<td></td> ?

I am trying to fetch book rows by books' first letter with jquery $.post into my <td> </td> which is part of my table.

My table looks like :

<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>

Writing jquery code, I become successful but with $("#result_list").html(data) like this:

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

But I dont want to fetch all data as HTML and insert into <tbody> . I want to fetch data from ajax.php as separate variable. For example; book_name,logo,pages,author. And I want to insert them into <td> </td>.

My ajax.php is like this:

$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 } ?>

Is there any way to take each variable(i.e. $book->name) and insert it into <td></td> ?

最满意答案

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

你需要做一些事情。 首先在$ .post()调用中为dataType为“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 ); }

Not only can you, but you should. Rather than having PHP return html as text, have it return a JSON object.

You're going to need to do a few things. First add a fourth argument to your $.post() call for a dataType of "json".

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

Then, its good form to have PHP send JSON headers for the result so before you send out any data in PHP use

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

And rather than echoing out the properties of your book model, just use json_encode()

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

So the client now has the raw data and you can use js/jQuery to loop through the books and add it to your table.

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 ); }有没有办法将ajax数据作为单独的变量而不是完整的HTML获取?(Is there any way to fetch ajax data as separate variable instead of full HTML?)

我试图通过书籍的第一个字母和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-> name)并将其插入<td></td> ?

I am trying to fetch book rows by books' first letter with jquery $.post into my <td> </td> which is part of my table.

My table looks like :

<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>

Writing jquery code, I become successful but with $("#result_list").html(data) like this:

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

But I dont want to fetch all data as HTML and insert into <tbody> . I want to fetch data from ajax.php as separate variable. For example; book_name,logo,pages,author. And I want to insert them into <td> </td>.

My ajax.php is like this:

$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 } ?>

Is there any way to take each variable(i.e. $book->name) and insert it into <td></td> ?

最满意答案

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

你需要做一些事情。 首先在$ .post()调用中为dataType为“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 ); }

Not only can you, but you should. Rather than having PHP return html as text, have it return a JSON object.

You're going to need to do a few things. First add a fourth argument to your $.post() call for a dataType of "json".

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

Then, its good form to have PHP send JSON headers for the result so before you send out any data in PHP use

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

And rather than echoing out the properties of your book model, just use json_encode()

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

So the client now has the raw data and you can use js/jQuery to loop through the books and add it to your table.

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 ); }