帖子值未传递到操作页面。操作页面仅返回静态回复,即没有$ _POST部分的感谢消息。
以下是代码。
JQuery的
<script>
$(function() {
$('#myF').submit(function (e) {
e.preventDefault();
$('#contactForm').html('<img src="images/ajax_small.gif" />');
subMe();
});
});
function subMe() {
$.post('?action=contactdetails', $('#myF').serialize(),
function(data) {
$('#contactForm').html(data.returnValue);
}, "json");
}
</script>
HTML
<form id="myF">
<div id="contactForm" valign="top" style="width:417;text-align:center" class="main">
<table width="417" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="45"><span class="style3">Name</span></td>
<td width="373" height="30"><input name="name" id="name" style="padding-left:5px" type="text" title="Name is required." class="required form" /></td>
</tr>
<tr>
<td height="14" colspan="2"><img src="images/spicer.gif" width="1" height="1" /></td>
</tr>
<tr>
<td><span class="style3">Email</span></td>
<td width="373" height="30"><input name="email" title="Please enter a valid email id." id="emailid" style="padding-left:5px" type="text" class="validate-email required form" /></td>
</tr>
<tr colspan="2">
<input type="submit" value="Submit" />
</tr>
</table>
</div>
</form>
行动页
<?php
if ( $action == 'contactdetails' ) {
$data['layout'] = 'layout_blank.tpl.php';
$nme = $_POST['name'];
echo json_encode(array("returnValue"=>"<strong>Thank you " . $nme . " for contacting us.<br /> We will get back to you soon.</strong>"));
}
?>
答案 0 :(得分:1)
您的输入位于<div id="contactForm">
元素中,直到为止这一行:
$('#contactForm').html('<img src="images/ajax_small.gif" />');
此时他们消失了。一旦发生这种情况,调用.serialize()
将不会返回任何值,因为没有任何输入可供查找。
首先调用subMe()
,然后更改<div>
元素的内容:
$(function() {
$('#myF').submit(function (e) {
e.preventDefault();
subMe();
$('#contactForm').html('<img src="images/ajax_small.gif" />');
});
});