Javascript Ajax jquery获取

Javascript Ajax jquery获取,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我想知道如何将Ajax合并到我的PHP代码中,使内容动态加载 现在是这样的。 首先,用户选择一个类别: <li><a href='?genre=sport'>Sport</a></li> 最终导致在某种html方案中呈现结果: while(mysqli_stmt_fetch($result)) { echo $id; echo $title, echo $artist; } 我知道我可能会通过$.ajax发送GET JSON数据,然后

我想知道如何将Ajax合并到我的PHP代码中,使内容动态加载

现在是这样的。 首先,用户选择一个类别:

<li><a href='?genre=sport'>Sport</a></li>
最终导致在某种html方案中呈现结果:

while(mysqli_stmt_fetch($result))
{
     echo $id; echo $title, echo $artist;
}
我知道我可能会通过$.ajax发送GET JSON数据,然后检索并通过服务器上的mysqli运行它。 我想知道的是,将数据返回html的最佳方法是什么,以及如何将其放置在某种重复结构中。
我似乎无法理解这件事。谢谢。

如果您不使用纯ajax,您可能会看到。您可以使用以下代码来模拟您的案例

<?php
include './path/to/xajax.inc.php';

$xajax = new xajax();

$xajax->register(XAJAX_FUNCTION, 'getGenre');

$xajax->processRequest();

function getGenre($a, $b)
{
    $response = new xajaxResponse();
    // your db code here
    $response->assign('result', 'innerHTML', $result);
    return $response;
}

谢谢,我将查找xajax并尝试一下。代码看起来非常整洁和有条理!当它起作用时,我们将感谢您的支持和确认:)不幸的是,我正在寻找一个Ajax解决方案,但如果没有其他人回答,出于礼貌,我将予以确认。@user3428402好的,您可以看到我更新的答案。简单地说,从php返回json,并使用jquery模板在js端呈现它
<?php
include './path/to/xajax.inc.php';

$xajax = new xajax();

$xajax->register(XAJAX_FUNCTION, 'getGenre');

$xajax->processRequest();

function getGenre($a, $b)
{
    $response = new xajaxResponse();
    // your db code here
    $response->assign('result', 'innerHTML', $result);
    return $response;
}
<li><a href='?genre=sport' onclick="getGenre('sport')">Sport</a></li>

<div id="result"></div>
<?php
if(isset($_GET['genre']))
{
    $genre=$_GET['genre'];
}
$result=mysqli_prepare($connection,'SELECT * FROM songs WHERE genre=?');
mysqli_stmt_bind_param($result,'s',$genre);
mysqli_stmt_execute($result);
mysqli_stmt_bind_result($result,$id,$title,$artist);

#arr = array();

while ( mysqli_stmt_fetch($result) ) {
    $arr[] = $id;
}

return json_encode($arr);
<ul id="result"><ul>
$("a").on("click", function() {
    $.getJSON( "your.php" + $(this).attr("href"), function( data ) {
        var template = "<li><b>${id}</b> <b>${title}</b> <b>${artist}</b></li>";

        $.template( "yourTemplate", template );
        $.tmpl( "yourTemplate", data )
          .appendTo( "#result" );
    });
});