我正在尝试使用AJAX发布,因为我不想使用提交按钮并在每次点击它时重新加载页面。 我将此代码用于ajax:
<script language="JavaScript"><!--
function postit()
{
var frm = $('#pmconfirm');
$.ajax({
type: "POST",
url: "bitcin",
data: frm.serialize(),
success: function(msg){
$("#main").hide();
$("#main").html(msg).show();
},
error: function(msg){
$("#main").html("<font color='#ff0000'>Ajax loading error, please try again.</font>").show();
}
});
}
setTimeout("postit()",2000);
//--></script>
接下来,我正在使用此表单:
<form action="" name="fcaptcha" method="post">
<input type="hidden" id="bitcoin" name="bitcoin">
<input type="hidden" id="pmconfirm" name="pmconfirm" src="http://www.mvixusa.com/newsletter/2010/11/newsletter-membership-confirmation/images/confirm-button.png" alt="Submit Form" onclick=\"document.getElementById("fcaptcha").submit()\"/>
</form>
<div id="main">
</div>
这可以发布,但我没有给我结果?
if (isset($_POST['bitcoin']))
{
// My code here works, because it works when i dont use ajax
// And I have some things to check like if it wasnt what i wanted
// it returns some message which is shown with php.
}
<div id="messaget">
<?php
if($failed == 1) echo $messages;
?>
</div>
这是应该显示消息的部分,我尝试使用标签#messaget在发布后显示HTML但是它没有工作,我尝试在此页面显示整个页面它仍然没有&#39工作。
网址:&#34; bitcin&#34;,完全没问题,我使用了htaccess。 有人可以找出问题所在吗?
答案 0 :(得分:3)
在表单中添加ID:
<form id="pmform" action="" name="fcaptcha" method="post">
将Js改为:
var frm = $('#pmform');
执行时:
............
data: frm.serialize(), //this will take the form and make an array based on the names of the form elements thus having them accessible in the PHP script
..........