简体   繁体   中英

Jquery validate plugin, How to check if PHP file returns an error or ok to redirect user to homepage/dashbord

First off this is my first question on here so I am sorry for anything I may do wrong. But....

My question is how do I take whats sent back from my PHP file to see if username/password combo exists in the database. My PHP either returns true or false depending on the submitting. That works fine I just do not know how to use the returned data and compare it to either true or false.

////This is my Jquery of my form which works fine validating and sending data to PHP.

$("#frmsignin").validate({
            rules:{
                passin:"required",
                emailin:{
                required:true,
                email:true
            }
        },
            messages:{
                passin:"Please enter YOUR password",
                emailin:{
                    required:"Please enter YOUR email address",
                    email:"Please enter a VALID email address"
                }
        },
            submitHandler: function(form) {
                $.post('sign_in.php',
                $("#frmsignin").serialize(), 
                function(data){$('#results').html(data)});
            }                   
    });

Right Now I just have my PHP file printing back true/ false just to get the error message or redirection to occur if user signs in correctly.

Simple: Return a json data structure:

<php

$data = '... the stuff you want to return';

if ($data == '') {
    // oops something blew up
    $json = array('success' => false);
} else {
    $json = array('success' => true, $data);
}

echo json_encode($json);

Then in your JS code:

function(data) {
   var serverdata = jQuery.parseJSON(data);
   if (serverdata.success) {
       ... it worked
   } else {
       ... something blew up, handle it
   }
}

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