简体   繁体   中英

php code not redirecting to ajax calls

I have following js code, which is validating a form calling a php script at backend:

$(function() {

    // Setup form validation on the #register-form element
    $("#register_form").validate({

        // Specify the validation rules
        rules: {
            register_username: "required",
            register_password: "required",
            register_email: {
                required: true,
                register_email: true
            },
            register_confirm_password: {
                required: true,
                minlength: 5
            },
        },

        // Specify the validation error messages
        messages: {
            register_username: "Please enter your username",
            register_password: "Please enter your password",
            register_confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            register_email: "Please enter a valid email address",
        },

       submitHandler(function(form) {
            $.ajax({  
              type: 'POST',
              url: $(this).attr('action'),
              data: $(this).serialize(),
              dataType : 'json',
              success(function(data) {
                if (data){
                    alert("success");
                    // $(this)[0].reset();
                }
              })
            });
            return false;
        });
    });

});

At the backend I have following code:

var_dump($_POST);

The issue is in browser, I am successfully able to dump php data, but unable to call this function message in js:

submitHandler(function(form) {
    $.ajax({  
        type: 'POST',
        url: $(this).attr('action'),
        data: $(this).serialize(),
        dataType : 'json',
        success(function(data) {
            if (data) {
                alert("success");
                // $(this)[0].reset();
            }
        })
    });
    return false;
});

Please help me to solve the above code.

The problem is that var_dump($_POST); wont output valid JSON. Change it to this:

echo json_encode($_POST);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM