1

我按照tutsplus.com 上的提交 Ajax 表单教程进行操作,但终生无法弄清楚为什么我的数据不会应用 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();
?>
4

1 回答 1

0

尝试

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

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

于 2013-01-01T07:17:31.427 回答