简体   繁体   中英

Getting response from Php in javascript

I have a php script which forms part of a login process, I pass the data from the login page to the process.php page via ajax, now this works, but what i am trying to do is get a response back in the form of a 1 or a 0 and then generate an action based on the result.

here is my code.

 (document).ready(function(){
$('form.login').submit(function () {
var user = $(this).find("[name='user']").val();
var pass = $(this).find("[name='pass']").val();
var sublogin = $(this).find("[name='sublogin']").val();

// ...
    $.ajax({
type: "POST",
url: "http://www.server.co.uk/process.php",
data: {
    user : user,
    pass : pass,
    sublogin : sublogin,
},
 success: function(response){                       
        if(response == "1")
                    {
                        text = "Logged In";
                        /*
                            login successfull
                        */
                    }
                    // Login failed
                    else
                    {
                        text = "Error";
                    }


        //alert(response);
    }
});

And the process.php code

  function procLogin(){
  global $session, $form;
  /* Login attempt */
  $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));

  /* Login successful */
  if($retval){ 
  echo '1';

  }
  /* Login failed */
  else{
    echo '0';
  }

}

I think my problem is the way I am trying to pass the information back from the php page to the login page.

UPDATE: I can get a response back, using a response container in the javascript, i can echo the correct result for example 1 if the system logs in or 0 if it fails. What i can't seem to work out is how to take the responding result 1 or 0 and have it activate the if else statement. Sorry probably wasn't clear enough earlier.

do one alert in the result of the ajax. And see if it's ok with your PHP.

But I guess that is not going is because when accessing the php script is inside a function[procLogin()] that is never called.

remove the function and let loose code

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