如何使用Jquery重新加载PHP查询(包含页面)

时间:2014-09-26 10:51:46

标签: javascript php jquery ajax

我有一个小片段,这是一个评论系统。我试图这样做,当我按提交时,表单本身将刷新,评论也将如此。

我尝试过使用AJAX,但是当我按下"提交"时,我看不到任何实际启动的内容。 我的 frontpage.php 包含播放器的每个元素。 这是 player_comments.php:

的核心
    <script>

$(document).ready(function() { 
    var options = {
        url: '',
        target: '#comment-text', // target element(s) to be updated with server response
        type: 'post' // 'get' or 'post', override for form's 'method' attribute
    }; 

    // bind form using 'ajaxForm' 
    $('#song-comment-form').ajaxForm(options); 
}); 

</script>

<?
}
if(isset($userId)) {
    /* logged in only */
}

$iComments = 0;
$qComments = $db->query("
SELECT songs_comments.*, user.id AS uId, user.username AS uName, user.avatar AS uAvatar
FROM songs_comments LEFT JOIN user ON songs_comments.userid_from = user.id
WHERE songs_comments.songid = '".$rSong->id."' ORDER BY songs_comments.id DESC");
while ($rComments = $qComments->fetch_object()) {
    $showComments .= '
    <img src="../'.makeAvatar($rComments->uId,$rComments->uAvatar,50).'" class="avatar float-left" alt>
    <div class="comment">
        <div class="comment-text">'.$rComments->text.'</div>
        <div class="comment-footer">
            <a href="/">'.$rComments->uName.'</a> on '.dateStamp($rComments->time).'
        </div>
        <br style="clear:both;">
    </div>
    ';
    $iComments++;
} ?>
<div id="player-song-comments-wrap">
    <div id="player-song-comments-heading"><img src="template/images/icons/comments.png" alt> Comments</div>
    <div id="player-song-comments-sub-heading">
        <?=isset($userId)?'<a href="/" id="show-song-comment-form" class="float-right">Add comment</a>':'<a href="/register.php" class="modal float-right">Add comment</a>'?>
        <span id="song-comments-num"><?=$iComments?></span> comments for "<span id="song-comments-title"><?=$rSong->title?></span>"
        by <span id="song-comments-artist"><?=$rSong->artist?></span>
    </div>
    <hr>
    <form id="song-comment-form">
        <input type="hidden" value="<?=$rSong->id?>" class="song-id">
        <textarea class="editor" id="song-comment-textarea"></textarea><br>
        <input type="submit" value="Submit"><input type="button" value="Cancel" id="hide-song-comment-form">
        <hr>
    </form>
    <div id="player-song-comments">
        <?=$showComments?>
    </div>
</div>

如何点击提交,重新加载此内容中的所有内容?

1 个答案:

答案 0 :(得分:1)

这里你的ajax调用代码

&#13;
&#13;
<script>
        $(document).ready(function(){


            $("#submit_data").on('click',function(e){
                $.ajax({
                type:'POST',                   
                url:"player_comments.php",

                success:function(data){
                    console.log(data);
                    $("#player-song-comments-wrap").html(data)
                 }
            });
            });
        });
</script>
<form id="song-comment-form">
   <input type="hidden" value="<?php echo $rSong->id ?>" class="song-id">
   <textarea class="editor" id="song-comment-textarea"></textarea><br>
   <input type="submit" value="Submit" id="submit_data"><input type="button" value="Cancel" id="hide-song-comment-form">
   <hr>
</form>
 

<div id="player-song-comments-wrap">
  
</div>
&#13;
&#13;
&#13;

这里你的player_comments.php代码调用ajax url

&#13;
&#13;
<?php
if(isset($userId)) {
    /* logged in only */
}

$iComments = 0;
$qComments = $db->query("
SELECT songs_comments.*, user.id AS uId, user.username AS uName, user.avatar AS uAvatar
FROM songs_comments LEFT JOIN user ON songs_comments.userid_from = user.id
WHERE songs_comments.songid = '".$rSong->id."' ORDER BY songs_comments.id DESC");
while ($rComments = $qComments->fetch_object()) {
    $showComments .= '
    <img src="../'.makeAvatar($rComments->uId,$rComments->uAvatar,50).'" class="avatar float-left" alt>
    <div class="comment">
        <div class="comment-text">'.$rComments->text.'</div>
        <div class="comment-footer">
            <a href="/">'.$rComments->uName.'</a> on '.dateStamp($rComments->time).'
        </div>
        <br style="clear:both;">
    </div>
    ';
    $iComments++;
} 


?>
&#13;
&#13;
&#13;