2013-01-01 88 views
1

我跟着Submit Ajax Form tutorial on tutsplus.com, ,但無法弄清楚爲什麼我的數據不會有addreply.php應用於它的生活。當我查看我的mysql表時,數據不會被插入。任何幫助將不勝感激。我搜索了網頁,並排查了好幾個小時。Ajax表單不提交

$(document).ready(function() { 

$(".replyLink").one("click", function(){ 
$(this).parent().after("<div id='contact_form'></div>"); 
$("#contact_form").append("<form id='replyForm'></form>"); 
$("#replyForm").append("<input class='enterName' id='enterName' type='text' 
name='name' placeholder='name' rows='1' cols='20' />"); 
$("#replyForm").append("<textarea class='enterReply' id='enterReply' name='comment' 
placeholder='reply'></textarea>"); 
$("#replyForm").append("<input type='hidden' name='id' value=''>"); 

commentID= $(this).parent().attr('id'); 

$("#replyForm").append("<input class='replyButton' id='replyButton' type='submit' `value='reply'/>");` 
$(".enterReply").slideDown();         

$(".replyButton").slideDown(); 

}); 

$(".replyButton").click(function() { 

var name = $("input#enterName").val(); 
var reply = $("textarea#enterReply").val(); 



var dataString = 'name='+ name.val() + '&comment=' + reply.val(); 

$.ajax({ 
type: "POST", 
url: "addreply.php", 
data: dataString, 
success: function() { 
} 
}); 
return false; 
}); 

**addreply.php** 

<?php 
session_start(); 

$replyID= $_POST['id']; 

$name= $_POST['name']; 
$comment= $_POST['comment']; 
$type= $_POST['type']; 
$song= $_POST['song']; 

if($song == ''){ 
$song= 'not'; 
} 

include 'connection.php'; 

if($_SESSION['signed_in'] == 'yes') { 
$query1= "INSERT INTO ApprovedComments(name, comment, Authorized, type, reply_ID, song, date) 
VALUES('$name', '$comment', 'YES', '$type', '$replyID', '$song', NOW());"; 

$insertComment= mysql_query($query1); 
// echo "hi"; 
} 

if(!isset($_SESSION['signed_in'])) { 
$query2= "INSERT INTO PreApprovedComments(name, comment, reply_ID, song, date) 
VALUES('$name', '$comment', '$replyID', '$song', NOW());";  

$insertComment= mysql_query($query2); 
} 

mysql_close(); 
?> 
+0

檢查?檢查網絡選項卡上烏爾要求越來越提交服務器?最後但並非最不重要,我希望你已經在你的HTML中添加了jQuery庫.. – Rajesh

+0

感謝您的回覆。我檢查了所有這些東西。它最有可能不是那些非常簡單的錯誤之一。我點擊回覆按鈕後,頁面刷新並重定向到(website_name)/?name = sss&comment = sss&id =。不知道爲什麼這個信息顯示在url – Mouse6541

+0

後烏拉圭回覆按鈕的HTML代碼..按鈕點擊頁面不應刷新..因爲我可以看到返回false語句已添加點擊事件..如果頁面越來越刷新然後必須有一些錯誤在控制檯顯示在螢火蟲 – Rajesh

回答

0

嘗試

$.ajax({ 
    type: "POST", 
    url: "addreply.php", 
    data: $("#replyForm").serialize()+'name='+ encodeURIComponent(name) + 
      '&comment=' + encodeURIComponent(reply), 
    success: function() { 
    } 
}); 

這將張貼在#replyForm形式的所有字段的名稱和註釋字段。

+0

感謝您的答覆。雖然沒有工作。這真的很奇怪。 – Mouse6541

+0

@ user1940631刪除'var dataString ='name ='+ name.val()+'&comment ='+ reply.val();'這會導致錯誤導致您的表單提交(name是一個字符串並且沒有方法VAL())。 – Musa