0

I'm trying to create an ajax request. Here's my code :

function cek(){
        var username = $("#username").val();
        var password = $("#password").val();

        if (username == "" && password == "") {
            alert("Username and password can't be empty!");
        }else {
            $.ajax({
                url: "processLogin.php",
                type: "POST",
                data:{
                    username: $("#username").val(), password: $("#password").val()
                },
                success:function(e){
                    alert("e");
                }
            });
        }
    }

And here is the php code :

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

$sql = "SELECT * FROM user WHERE nama='$username' AND password='$password'";
$query = $db->query($sql)or die(mysqli_error($db));
$result = $query->fetch_assoc();

if ($query->num_rows == 0) {
    echo "Username not found!"; 
}elseif ($password != $result['password']) {
    echo "Password incorrect!";
}else{
    header('location:index.php');
}

I want my program to show an alert based on the result of if else. But it doesn't show any alert. I've search on google, but I still can't make it work.
Literally I'm new on using ajax. I hope someone can help me. Thank you.

kurniawan26
  • 793
  • 4
  • 16
  • 35

1 Answers1

-1

One of the things u need to do is remove the quotes on alert alert("e");

to be alert(e);

then you need to return prevent the form from submit/loading the page using prevent default, also return false when the username/pass are empty so that the form won't continue submiting.

<script type="text/javascript">
       function cek(event){
                 event.preventDefault();
                 var username = $("#username").val();
                var password = $("#password").val();
            if (username == "" && password == "") {
                alert("Username and password can't be empty!");
                return false;
            }else {

                   $.ajax({
                    url: "processLogin.php",
                    type: "POST",
                    data:{
                        username: $("#username").val(), password: $("#password").val()
                    },
                    success:function(e){
                        alert(e);
                    }
                });


            }
        }
    </script>



    <form method="POST">
        <input type="text" name="username" id="username">
        <input type="password" name="password" id="password">
         <button type="submit" class="button" id="login" onclick="cek(event)">Login</button>
    </form>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • Thank you mate. It works. I think I still make a mistake at the `else` statement. It shows an alert with the whole content of `header('location:index.php');` – kurniawan26 Apr 20 '17 at 15:50
  • No no don't do redirects using php, redirect with jquery/js on the frontend... redirecting with php will redirect the ajax request – Masivuye Cokile Apr 20 '17 at 15:53
  • no u need to remove `header('location:index.php');` on the php page then wait for the response on the ajax, if the response is what u want then redirect in front – Masivuye Cokile Apr 20 '17 at 16:00
  • thank you so much for helping. Now I can move on. :) – kurniawan26 Apr 20 '17 at 16:06