2

我有一个 HTML 表单

<form method="POST" action="" name="myform" id="myform">
    <input type="text" name="email" id="email" />
    <input type="supmit" name="submit" id="submit"  value="submit" />
</form>

还有一段jQuery代码:

$(document).ready(function(){
    var validator = $("#myform").validate({
        ignore: ".ignore",
        rules: {...},
        messages: {...},
        submitHandler: function(form) {
            $.post('usr.php?resetpw', $(this).serialize(), function (data, textStatus) {
                form.submit();
                alert(data.inf);
            },'json');
        },
});

还有没有得到 $_POST-variables ( isset($_POST['email'])= false)的 PHP 代码 usr.php

if(isset($_GET['resetpw'])) {
    $loggingData = array(
        'inf' => utf8_encode("Your email address is: ".$_POST['email']),
        'errorEmail' => '',
        'mailExists' => '',
        'success' => '',
    );
}
echo json_encode($loggingData);

将变量发布到 PHP 的正确代码是什么?

谢谢

4

3 回答 3

4

我刚刚validate从这里检查了插件的来源http://jquery.bassistance.de/validate/jquery.validate.js

submitHandler它是这样调用的

 validator.settings.submitHandler.call( validator, validator.currentForm );

这意味着this将引用validator objectnot the form,因此使用form参数来引用表单并序列化它的字段,如下所示

 $(form).serialize()
于 2012-06-02T10:52:49.610 回答
2

form不序列化this。所以 submithandler 将如下所示..

submitHandler: function(form) {
        $.post('usr.php?resetpw', $(form).serialize(), function (data, textStatus) {
            form.submit();
            alert(data.inf);
        },'json');
},
于 2012-06-02T10:51:51.770 回答
0
$("#form_id").trigger('submit');

试试这个

于 2012-06-02T12:27:39.937 回答