Skip to content

Instantly share code, notes, and snippets.

@stanleybz
Last active October 15, 2017 14:48
Show Gist options
  • Save stanleybz/ad795c599d9db68e0e28bf6d979ae190 to your computer and use it in GitHub Desktop.
Save stanleybz/ad795c599d9db68e0e28bf6d979ae190 to your computer and use it in GitHub Desktop.
<!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>
<div id="message"></div>
</section>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script>
$('#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.
if (data.result == 'success') {
$('#message').html('success');
} else {
$('#message').html('fail');
}
},
error: function(d) {
$('#message').html('error');
}
});
return false;
})
</script>
</body>
</html>
<?php
$users = array(
0 => array(
"username" => "admin",
"password" => "admin",
),
1 => array(
"username" => "demo",
"password" => "demo",
),
);
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
return;
}
}
}
$data = array('result' => 'fail', 'message' => 'Message not sent from server');
echo json_encode($data);
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment