Ajax表单没有提交

时间:2013-01-01 07:08:05

标签: ajax forms

我跟着Submit Ajax Form tutorial on tutsplus.com, 但无法弄清楚我的生活为什么我的数据不会应用addreply.php。当我查看我的mysql表时,数据不会被插入。任何帮助将不胜感激。我在网上搜索了几个小时的故障排除。

$(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();
?>

1 个答案:

答案 0 :(得分:0)

尝试

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

这将发布#replyForm表单中的所有字段以及名称和评论字段。