0

I'm trying to create a login script where the user will enter their username and password. This will be sent via ajax to a php script to check if the details are correct. If they are the user should then be able to select a site, which ive been trying to display like this $('#logonContainer').load("login_DisplaySelectSite.php");

My problem is that I cant figure out how to run this code if the session has started with a valid username. Heres my code so far, maybe someone can help me!

html/jquery mobile:

<div data-role="content">   
    <form id='login' onsubmit="return false;">      
                <div id='logonContainer' style="background-color: #F8F4FA; padding: 10px;  width:70%; margin: 0 auto; margin-top:15%;">
                    <input type="text" name='user' id='user' placeholder='Username'>
                    <input type="password" name='password' id='password' placeholder='Password'>
                    <input type="submit" id="submitLogin" name="login" value="Login" class='ui-btn ui-btn-c ui-btn-inline' data-theme='c' data-transition='pop'>
                </div>
            </form>
    <div id="resultLog"></div>
</div>

ajax:

$(function userLogin() {

        $("#submitLogin").click(function() {
            var theUser = $.trim($("#user").val());
            var thePassword = $.trim($("#password").val());

            if(theUser.length > 0)
            {
                $.ajax({
                  type: "POST",
                  url: "callajax.php",
                  data: ({usr: theUser, pwd: thePassword}),
                  cache: false,
                  dataType: "text",
                  success: onSuccess
                });
            }
        });

        $("#resultLog").ajaxError(function(event, request, settings, exception) {
          $("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
        });

        function onSuccess(data)
        {
            $("#resultLog").html(data);


        }

This next bit is not working at all!! This is where I'm trying to display a radio button list to replace the username and passowrd fields. After this I guess I will use a similar function to select the site, again, dependant on the session being active - once all these checks have been done the user should be sent to the home page

        if(onSuccess(data) == 1) 
        {
            $('#logonContainer').load("login_DisplaySelectSite.php");

        } else {

        }

    });

php:

session_start();
if($_REQUEST['usr']=="admin" && $_REQUEST['pwd']=="1234"){

    $_SESSION['USERNAME'] = "admin";
    $_SESSION['SESSIONID']='123456';

    echo "Login CORRECT WOOOOOOOO!! </br>" ;
    //print json_encode($_SESSION);
    print $_SESSION['USERNAME'];
    $loggedon=1;
    echo json_encode( $loggedon);

}
else{
    echo "Incorrect Login :(" ;
    $loggedon=0;
    echo  json_encode($loggedon);
}

Thanks

1
  • In your AJAX function you're not passing anything to the onSuccess() function. Commented May 30, 2014 at 12:28

2 Answers 2

1

I see data isn't defined yet? And you might want to add this line too: data: $( "#login" ).serialize() in $.ajax();

Sign up to request clarification or add additional context in comments.

2 Comments

how should I define data? Sorry for my ignorance I'm a total ajax newbie!
Did you try to add the line data: $( "#login" ).serialize() ?
0

You are missing the signature of the success event. Just add the parameter to Success: success: onSuccess(data) and try to check, it should work fine.


to check if the success, event is triggered its better to use some debugging tools or alerts/console messages/print to screen etc based on your development environment and IDE that you use, that will tell you if its working fine.

thanks.

3 Comments

this stopped the script from returning anything?!
if you are not getting any data/result that means you are not receiving any response from the server/requested page. if(onSuccess(data) == 1) call will not work as it onSuccess is not a method, its an event. so if you want to execute any code based on the return/response you should write inside the onSuccess event/defination.
@Janey : if you want to use the response all over the page/js you should store it in var eg: var _response; and use this _response variable to verify or validate against any. --Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.