I have a login form like this:
I have 3 files: index.html
, login.php
, custom.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<section>
<div id="message"></div>
<form id="loginform" method="POST">
<p>Username: <input type="text" id="username" name="username" size=25 />
</p>
<p>Password: <input type="password" id="password" name="password" size=25 />
</p>
<button id="login" type="submit" name="submit">Login</button>
</form>
</section>
</body>
</html>
custom.js
looks this way:
$('#login').click ( function() {
$.ajax({
type: "POST",
url: 'login.php',
data: $('#loginform').serialize(),
dataType: 'json',
success: function(data) {
//alert(data.message);
// print into ( <div id="message"></div>) on file html.
},
});
return false;
});
login.php
looks this way:
$users = array(
0 => array(
"username" => "admin",
"password" => "admin",
),
1 => array(
"username" => "demo",
"password" => "demo",
),
);
Please check my code below:
for ( $i=0; $i< count($users) ; $i++ ){
if ( isset($_POST['username'])&& isset($_POST['password']) ) {
// Please check this code
if ( $_POST['username'] == $users[$i]['username'] && $_POST['password'] == $users[$i]['password'] ) {
$data = array('result' => 'success','message' => 'Message sent from server');
echo json_encode($data);
// Please check this code
} else {
// Please check this code
$data = array('result' => 'fail', 'message' => 'Message not sent from server');
echo json_encode($data);
}
}
}
I want to check user (array) with users from Form login, use AJAX print message if match user or not.
Please help me check all my code and fix it. Thanks all.