简体   繁体   中英

How can I check on a result from a php script that is returned with ajax

I got a login script (not yet encrypted and all) which posts to a PHP script, this script then returns a result, either that the credentials are correct, that they are wrong or that a user needs to fill in 1 or both of the fields when empty.

After this I want to redirect to a page according to the result (if the credentials are correct). But not of course when it is wrong or when 1 or both of the fields are empty.

How can I check for that in ajax?

This is what I have now in my PHP script:

$conn = new Connection;

$username = $_POST['username'];
$userpassword = $_POST['userpassword'];

if(empty($username) && empty($userpassword)){
  echo 'Vul een gebruikersnaam en wachtwoord in';
}else if(empty($username)){
  echo 'Vul een gebruikersnaam in';
}else if(empty($userpassword)){
  echo 'Vul een wachtwoord in';
}else{
  //Both filled in, begin logincode:
  $getuser = "SELECT * FROM users WHERE username = '".$conn->real_escape_string($username)."'";
  $getusercon = $conn->query($getuser);
  $getuser = $getusercon->fetch_assoc();

  if($userpassword == $getuser['password']){
    if($getuser['rights'] == '1'){
      $_SESSION['user'] = 'admin';
       $userdata = array(
         'userdata' => $_SESSION['user'],
      );
     echo json_encode($userdata);
    }else{
      $_SESSION['user'] = 'user';
       $userdata = array(
         'userdata' => $_SESSION['user'],
      );
     echo json_encode($userdata);
    }
  }else{
    echo 'Wachtwoord en gebruikersnaam komen niet overeen';
  }
}

This is my ajax:

// Login Ajax Code
$( "#content" ).on("submit", "#loginform", function( event ) {
  // Stop normal form behaviour
  event.preventDefault();
  // Retrieve input fields and their values
  var $form = $( this ),
  $username = $form.find( "input[name='username']" ).val(),
  $userpassword = $form.find( "input[name='userpassword']" ).val(),
  url = $form.attr( "action" );
  // Post above values to the action of the form
  var posting = $.post( url, { username: $username, userpassword: $userpassword} );
  // Show result in a div
  posting.done(function( data ) {
    $( "#loginresult" ).empty().slideDown('fast').append( data );
  }, "json");
});

I added the last , "json" part to try and get the session content back, which kind of works.

I see this in my loginresult box when I login correctly with an admin account: {"userdata":"admin"}

How can I decode that to use in my ajax code? The problem is I only need to decode it when it's json of course, not when it's a normal message that shows one or both fields are empty. Maybe all responses need to be json?

How can I for example redirect to a certain page when userdata contains admin ?

All the responses must have the same format, JSON in this case. So, in Javascript, you can do something like:

obj = JSON.parse(data);

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