I want to do is make a login system without refreshing the page using a button
, ajax but i dont know how to do it.
My problem is my code wont work if i click the button. How do i use ajax to communicate with my myIncludes.php if i press the button login?
index.php
<html lang="en">
<head>
<script type="text/javascript">
$('#login').click(function(){
<?php include_once('myIncludes.php'); ?>
});
</script>
</head>
<body>
<form method="post">
Username: <input type="text" name="user" /><br />
Password: <input type="text" name="pass" /><br />
<button type="button" name="login" id="login">Login</Button>
</form>
</body>
</html>
myIncludes.php
<?php
session_start();
include_once('connection.php');
include_once('user.php');
if(isset($_POST['submit'])){
$user = $_POST['user'];
$pass = $_POST['pass'];
$object = new User();
$object->Login($user, $pass);
}
?>
user.php
<?php
include_once('connection.php');
class User{
private $db;
public function __construct(){
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function Login($user, $pass){
if(!empty($user) && !empty($pass)){
$st = $this->db->prepare("SELECT * from users WHERE username=? AND password=?");
$st->bindParam(1, $user);
$st->bindParam(2, $pass);
$st->execute();
if($st->rowCount() == 1){
echo "User verifies, Access granted";
} else {
echo "Incorrect Username or Password";
}
}else{
echo "Please enter Username and Password";
}
}
}
?>
connection.php
<?php
class Connection{
public function dbConnect(){
return new PDO('mysql:host=localhost; dbname=test', 'root', '');
}
}
?>