我的AJAX帖子表单似乎不起作用。似乎没有数据通过。知道为什么吗?
表格
<label for="title">Please give your idea a title</label>
<br />
<input type="text" id="title" name="title" />
</p>
<p>
<label for="message">Please provide details of your idea</label>
<br>
<input type="text" id="message" name="message" />
</p>
<input type="button" id="sendmessage" value="Post"></input>
的jQuery
$.post("assets/post.php", {
title: $("#title").val(),
message: $("#message").val()
}, function (data) {
alert(data);
$chat.prepend('<span class="idea"><strong style="color: #0081C5">' + $messageTitle.val() + '</strong>' + ' - ' + $messageBox.val() + ' ' + '<a class="delete" href="#">[Delete]</a>' + '</span>')
$messageTitle.val('');
$messageBox.val('');
$('#post_confirm').show().html('<br><span class="confirm_msg">Message posted successfully</span>');
$('#chat').height($(window).height() - $('.header').height() - $('.user').height());
setTimeout(function () {
$('#post_confirm').fadeOut('slow');
$('#chat').height($chatHeight_user);
}, 1000);
});
post.php中
<?php include("config.php");
$title = $_POST["title"];
$message = $_POST["message"];
$sql = "INSERT INTO idea (title,message) VALUES (:title,:message)";
$q = $pdo->prepare($sql);
$q->execute(array(':title'=>$title,':message'=>$message));
?>
我收到以下错误消息:
答案 0 :(得分:2)
你没有任何触发器来开始这个后期动作。
$('#sendmessage').on('click', function(event)
{
event.preventDefault();
$.ajax(
{
url: "demo_test.txt",
data: yourdatahere,
success:function(result)
{
successevents here
}
});
});
现在它会收听点击发送消息的ID。如果单击它会禁用默认按钮行为并改为处理您的AJAX调用。