简体   繁体   中英

Get user profile on login with AJAX and PHP

I want to get the user profile data on login with AJAX and PHP, without reloading the page. Here is my login.php code. All I have to is if the login is successful than I want to alert the user's name and email.

<?php    
  include 'connection.php';    

  $login_mobile = $_POST['login_mobile'];
  $login_password = $_POST['login_password'];

  $check = mysqli_query($connection, "select * from sign_up_users where Mobile = '$login_mobile' and Password = '$login_password'");
  if (mysqli_num_rows($check) > 0)
  {
    $_SESSION['login_mobile'] = $login_mobile;
    echo json_encode(array("statusCode"=>200));
  }
  else
  {
    echo json_encode(array("statusCode"=>201));
  }     
  mysqli_close($connection); `    
?>
function make_login() {
  var login_mobile = $('#login_mobile').val();
  var login_password = $('#login_password').val();
  if (login_mobile != "" && login_password != "") {
    $.ajax({
      url: "php/login.php",
      type: "POST",
      data: {
        login_mobile: login_mobile,
        login_password: login_password
      },
      cache: false,
      success: function(dataResult) {
        var dataResult = JSON.parse(dataResult);
        if (dataResult.statusCode == 200) {
          // if success than i want to store name and email in var of login user
          alert("hurray!!!!");
        } else if (dataResult.statusCode == 201) {
          alert('invalid login');
        }
      }
    });
  } else {
    alert('Please fill all the field !');
  }
}

Here is your php code

<?php    
  include 'connection.php';    

  $login_mobile = $_POST['login_mobile'];
  $login_password = $_POST['login_password'];

  $check = mysqli_query($connection, "select * from sign_up_users where Mobile = '$login_mobile' and Password = '$login_password'");
  if (mysqli_num_rows($check) > 0)
  {
    $_SESSION['login_mobile'] = $login_mobile;

    // you can just add name or email detail in return array to ajax request

    echo json_encode(array("statusCode"=>200,'name'=>$check['name'],'email'=>$check['email'])); 
  }
  else
  {
    echo json_encode(array("statusCode"=>201));
  }     
  mysqli_close($connection); `    
?>

Here is you ajax jquery code

 function make_login() {
  var login_mobile = $('#login_mobile').val();
  var login_password = $('#login_password').val();
  if (login_mobile != "" && login_password != "") {
    $.ajax({
      url: "php/login.php",
      type: "POST",
      data: {
        login_mobile: login_mobile,
        login_password: login_password
      },
      cache: false,
      success: function(dataResult) {
        var dataResult = JSON.parse(dataResult);
        if (dataResult.statusCode == 200) {
          // if success than i want to store name and email in var of login user

          // You can access name and email like below

             var name = dataResult.name;
             var email = dataResult.email;

          alert(
                 "name="+name+"<br/>"+"email = "+email
               );
        } else if (dataResult.statusCode == 201) {
          alert('invalid login');
        }
      }
    });
  } else {
    alert('Please fill all the field !');
  }
}

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