I am designing a web application. It was working all fine before adding ajax. Now i want to add ajax but nothing is happening same first page is loading again and again. Please tell me the reason why ajax is not responding (no error message no new page loading). My code is below. h.php is a simple file saying hello.
first.php:
<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Online library </title>
<link rel="stylesheet" type="text/css" href="CSS/style.css" />
<form method="post">
<div class="result"></div>
UserName:<input type="text" name="name" id="username"><br><br>
Password:<input type="password" name="password" id="password"><br><br>
<button id="loginn" type="submit" name="button" <strong>login</strong> </button>
</form>
<script type="text/javascript" src=""jquery-3.2.0.js""></script>
<script type="text/javascript">
$(document).ready(function(){
$("#loginn").submit(function(e){
var username=$("#username").val();
var password=$("#password").val();
var result=$('.result').val();
var dataString = 'username='+username+'&password='+password;
$.ajax({
type: "POST",
url: "log.php",
data: dataString,
success: function(responseText){
if(responseText==0) {
result.html('<span class="error">incorrect username</span>');
}
else if(responseText==1)
{
window.location="h.php";
}
else{
alert('error in query');
}
}
});
e.preventDefault();
});
});
</script>
</body>
</html>
log.php:
<?php
include 'user.php';
$name=$_POST["username"];
$password=$_POST["password"];
$con=new users();
$con->login($name,$password);
?>
user.php:
<?php
class users
{
public function constructs()
{
$con=mysqli_connect("localhost", "root", "") or die (mysqli_error());
mysqli_select_db($con,'online') or die(mysqli_error($con));
return $con;
}
function login($username, $password) {
$con=$this->constructs();
$rs = mysqli_query($con, "SELECT username, password FROM member WHERE username='$username' AND password='$password'");
if(mysqli_num_rows($rs)>0)
{
echo 1;
}
else {
echo 0;
}
}
?>