Ok so I want to submit a login page without redirection. And after reading stuff about this, I tried AJAX. So I watched some tutorials about login page using ajax.
My reference is this video: https://www.youtube.com/watch?v=MkGUL7ZRCTA
What I want to do is that the user needs to login first. But I don't want the page to be redirected. Just check if the password matches the password in the json file. But when I try it, nothing is showing in the console log (I'm using firefox).
I want to do something like this: http://susheel61.0fees.net/ajaxtest.php but instead of showing the user input under it, it would show "Correct Password" or "Incorrect Password" (after checking the match)
Here are my code snippets: HTML
<!DOCTYPE html>
<html>
<head>
<title> AJAX Tutorial</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style type="text/css">
#loginbox{
width: 240px;
height: 140px;
margin: 0px auto;
margin-top: 300px;
}
</style>
</head>
<body>
<div id="loginbox">
<input id="password" type="password" placeholder="Enter Password" />
<button> Submit </button>
</div>
<div id="container"></div>
<script type="text/javascript">
$("button").click(function (){
var password = $("#password").val();
$.post("server.php", {password:password}, function(data){
console.log(data);
});
});
</script>
</body>
</html>
PHP
<?php
if (isset($_POST["password"])){
$password = $_POST["password"];
$jsondata = file_get_contents("CONFIG.json");
$json = json_decode($jsondata, true);
if ($password == $json["password"]) {
echo "Welcome";
} else {
echo "Wrong Password";
}
}
?>
and my JSON
{"reset":"12312","sync":"232131","config":"2","bypass":"22","password":"qwerty"}
EDIT: I tried using windrunn3r's answer. Everything works fine. But then, I want it that if the password is correct, it would show the (hidden) form. Here is my new HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> AJAX Tutorial</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style type="text/css">
#loginbox{
width: 240px;
height: 140px;
margin: 0px auto;
margin-top: 300px;
}
</style>
</head>
<body>
<div id="loginbox">
<div id="message"></div>
<input id="password" type="password" placeholder="Current Password" />
<button> Submit </button>
<div id="container"></div>
</div>
<div id="newpass" style="display: none">
<input id="password" type="password" placeholder="New Password" />
<button> Submit </button>
</div>
<script type="text/javascript">
$("button").click(function (){
var password = $("#password").val();
$.ajax({
url: 'server.php',
cache: false,
async: true,
beforeSend: function () {
$("#message").html("Checking...");
console.log(password)
},
type: "POST",
data: { password: password }, //data to post to server
error: function (data) {
$("#message").hide();
$("#container").html("No password was sent");
},
success: function (data) {
$("#message").hide();
console.log(data)
if (console.log(data)=="Welcome"){
$("#newpass").show();
} else {
$("#container").html("Password Incorrect");
}
}
});
});
/*$("button").click(function (){
var password = $("#password").val();
$.post("server.php", {password:password}, function(data){
console.log(data);
if (console.log(data) == "Welcome") {
document.getElementById("container").innerHTML = "Password Correct!";
} else {
document.getElementById("container").innerHTML = "Password Incorrect!";
}
});
});*/
</script>