2014-04-29 13 views
-1

該表單的工作原理與其中的一樣,除了一個問題,註釋不在$ postid之後。jQuery表單沒有對正確的帖子作出迴應

說有6個帖子和用戶對帖子3的評論,帖子3評論被添加到帖子1而不是帖子3. 換句話說,評論只會被添加到第一篇文章。

的意見需要按照每個單獨的職位,並且該數據位於變量$帖子ID。另外兩個變量是給用戶的。

javascript代碼:

<script type="text/javascript"> 
function submitForm() 
{ 
    $.ajax(
    { 
     type:'POST', 
     url: '<?php echo APP_ROOT; ?>views/layouts/ajaxtest.php', 
     data:$('#commentForm').serialize(), 
     success: function(response) 
     { 
      $('#commentForm').find('.form_result').html(response); 
     } 
    }); 
    return false; 
} 
</script> 

這些是傳遞到形式在PHP中的變量。

<?php 
    $postid = 'contains post id'; 
    $user = 'contains user code'; 
    $otheruser = 'contains other user code'; 
?> 

這是表單。

<form id="commentForm" onsubmit="return submitForm();"> 
    <input id="postid" name="postid" value="<?php echo $postid; ?>" type="hidden" /> 
    <input name="user" value="<?php echo $user; ?>" type="hidden" /> 
    <input name="friend" value="<?php echo $otheruser; ?>" type="hidden" /> 
    <textarea name="send[mc_comment]" placeholder='Comment' rows='1'></textarea> 
    <input type="submit" name="submit" value="Submit" onclick="submitForm();" class="postit" /> 
    <div class="form_result"> </div> 
</form> 
+0

當您查看源代碼,是在'postid'領域填寫正確? – Barmar

+0

它的問題與你的SQL Query.Post你的SQL查詢 –

+0

是的填寫正確的字段。該查詢正確插入數據。 $ postid根據每個帖子進行更改,僅在第一篇文章之後。 – user2433125

回答

0

這聽起來像問題是,您的所有帖子都使用相同的id="commentForm"。 ID在網頁中必須是唯一的。您應該使用類而不是ID來重複塊。

<form id="commentForm" onsubmit="return submitForm(this);"> 

然後改變JS如下:

function submitForm(form) 
{ 
    $.ajax(
    { 
     type:'POST', 
     url: '<?php echo APP_ROOT; ?>views/layouts/ajaxtest.php', 
     data:$(form).serialize(), 
     context: form, // So that success function can access the correct form 
     success: function(response) 
     { 
      $(this).find('.form_result').html(response); 
     } 
    }); 
    return false; 
} 
+0

非常感謝您的解決方案。 – user2433125