How do I echo PHP to Ajax? Login System

2

I'm working on a sign-in system that warns the user if the username already exists in the database. Basically I have a popup window on the same page where the input fields are for both login and user registration. The idea was to submit the fields and return a paragraph that would inform the user whether the account had been added successfully or not.

Here is the code:

 if(isset($_POST['submitlogin'])) {

        global $connection;
        $username = mysqli_real_escape_string($connection, trim($_POST['username']));
        $password = mysqli_real_escape_string($connection, trim($_POST['password']));

        if(!empty($username) && !empty($password)) {

            /*Register*/
                if(isset($_POST['classmember'])){
                    $class_member= mysqli_real_escape_string($connection, trim($_POST['classmember']));

                    $query = "SELECT member FROM members WHERE member='$username';";
                    $result = mysqli_query($connection, $query);
                    $num_rows = mysqli_num_rows($result);

                    if($password == GUILDPASS) {
                         if($num_rows == 0) {

                         $query2 = "INSERT INTO members(member,pass,class_id,status_id) VALUES ('$username',". '\''. GUILDPASS . '\'' . ",$class_member,3);";

                         $insert = mysqli_query($connection, $query2);

                         echo "Added Successfully";

                         } else {
                             echo "player already exists";
                         }
                     } else {
                         echo "Wrong password";
                     }
                }        
            /* LOGIN*/

                else {    
                    $query = "SELECT member,pass FROM members WHERE member='$username' AND pass='$password';";
                    $result = mysqli_query($connection, $query);
                    $num_rows = mysqli_num_rows($result);

                    if($num_rows == 0) {
                         echo "Create Account or Username/Password incorrect..";   
                    } else if ($num_rows == 1){
                         $_SESSION['sessionid'] = session_id();
                    }
                }


        } else {
            echo "Password/Username missing";
        }
     header("Location: ../index.php");
     exit();
}
    
asked by anonymous 28.06.2017 / 19:00

1 answer

2

You can get the value returned and use it in ajax, for example:

$.ajax({
  type: 'POST',
  url: 'SUA URL',
  dataType: 'html',
  success: function(data){
     alert(data)
  }
});

The date is the echo you returned from PHP, the above example is using the dataType as html , but we usually use it as json and return json of PHP with json_encode()

    
28.06.2017 / 19:05