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.