[Solved] Login not working… where is the error in PHP? [closed]


Heres my suggestion for you. Hopefully this makes sense and works for you. Please try to understand what is happening rather than just copy and hope it works. Your form, on submit, gets sent to the JS file. This grabs the form values, uses JSON to send them to your php file which processes everything and returns a response, either true or false. If false, the js file will display your errors on screen and add classes etc. If true you can get it to redirector do whatever you want.

HTML form…

<h1>Login</h1>
        <p id="results"></p>    
<form action="login_ajax.php" method="post" id="login" onsubmit="jsquicksub(this); return false;>
                <p id="emailP">Email Address: <input type="text" name="email" id="email" /><span class="errorMessage" id="emailError">Please enter your email address!</span></p>
                <p id="passwordP">Password: <input type="password" name="password" id="password" /><span class="errorMessage" id="passwordError">Please enter your password!</span></p>
                <p><input type="submit" name="submit" value="Login!" /></p>
    </form>

In your login.js

// hide the div where the error reporting gets displayed on load of the page
$(document).ready(function() {
    $('#results').hide();
});    

var gb_ajaxbusy = false;

function jsquicksub(po_form) {

    if (gb_ajaxbusy)
        return false;

    gb_ajaxbusy = true;


    var lo_request = {};
    // go through each and every form element and assign to variable
    lo_request.e = $(po_form).find('#email').val();
    lo_request.p = $(po_form).find('#password').val();

    $(po_form).fadeTo(200, 0.3);

    $.getJSON('/login.php', lo_request, function(po_result) { // php form processing field

        gb_ajaxbusy = false;
        $(po_form).fadeTo(0, 1);

        if (!po_result.success) {
            $('#results').show();
            document.getElementById('results').innerHTML = po_result.content; 

// inserts errors in specified div. can use alert if wanted
// do your thing with adding classes etc if you want to
            return false;
        }
        // replace form with thankyou message/redirect somewhere or whatever you want to do

        // empty results element
        $('#results').hide();

    });

} // end jsquicksub()

Then in your login.php file ….

// function gets passed values after validation and returns a message to the user on screen - gets passed back to JS file which then returns stuff to the screen
function jsonreturn($pb_success, $ps_content = null) {

    $lo_result = (object) array('success'=>$pb_success, 'content'=>$ps_content);

    ob_clean();
    echo json_encode($lo_result);
    exit;

} // end jsonreturn()

    // these values are recieved from the javascript function
    $ps_email = isset($_GET['e'])? $_GET['e']: null;
    $ps_password = isset($_GET['p'])? $_GET['p']: null;


    if(empty($ps_email) OR empty($ps_password)) {
         // run your check_login stuff in here
         // use jsonreturn(false, 'message') for error OR when everything is fine and you have set the session, use jsonreturn(true, 'complete');
    }
else {
    jsonreturn(false, 'Your error message in here which gets added to the id="results" p tag');
}

solved Login not working… where is the error in PHP? [closed]