最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - jQuery validate, submitting with submitHandler - Stack Overflow

programmeradmin1浏览0评论

I have an HTML-form

<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>

And a code of 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');
        },
});

and also PHP-code usr.php that doesn't get $_POST-variables (isset($_POST['email']) = false)

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

What is a correct code that post variables reached to PHP?

Thank you

I have an HTML-form

<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>

And a code of 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');
        },
});

and also PHP-code usr.php that doesn't get $_POST-variables (isset($_POST['email']) = false)

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

What is a correct code that post variables reached to PHP?

Thank you

Share Improve this question edited Jun 2, 2012 at 15:26 user2428118 8,1244 gold badges46 silver badges73 bronze badges asked Jun 2, 2012 at 10:42 iffiff 1926 silver badges14 bronze badges 1
  • What kind of logging have you done? Have you checked the request in Firebug/Developer tools to see if the data is actually getting sent? Try $('#myform').serialize(), this may not be referring to the form inside that function. – Christian Commented Jun 2, 2012 at 10:44
Add a ment  | 

3 Answers 3

Reset to default 4

I just checked the source of validate plugin from here http://jquery.bassistance.de/validate/jquery.validate.js

and it's calling submitHandler like this

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

Which means this will refer to the validator object not the form, so use the form argument to refer to the form and serialize it's fields like this

 $(form).serialize()

Serialize form not this . so submithandler will looks like below..

submitHandler: function(form) {
        $.post('usr.php?resetpw', $(form).serialize(), function (data, textStatus) {
            form.submit();
            alert(data.inf);
        },'json');
},
$("#form_id").trigger('submit');

try this

发布评论

评论列表(0)

  1. 暂无评论