1

帖子值未传递到操作页面。操作页面仅返回静态回复,即没有 $_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>"));
    }
    ?>
4

1 回答 1

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" />');
    });
});
于 2013-02-26T10:03:15.937 回答