Within a webpage I'm trying to implement a button to prompt for the users password in order to logout. What I want to do is to use a javascript prompt to ask for it and then via AJAX send it to a php file which will verify if the password is correct and if it is it will return to index. What I've tried till now is the following:
HTML:
<button style="" type="submit" class="" onclick='checkPass()'>
Logout
</button>
<script>
function checkPass()
{
var usr = "<?php echo $usr = $_POST['usr']; ?>";
var pass=prompt("Please write your password","");
if (pass!=null && pass!="")
{
$.post("check.php", {pass: pass, usr:usr});
}
}
</script>
Check.php:
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$pass = mysql_escape_string($_POST['pass']);
$usr = mysql_escape_string($_POST['usr']);
$sql = mysql_query("SELECT * FROM USERS WHERE Usr='$usr' and Password='$pass'");
$count=mysql_num_rows($sql);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
header("location:index.php");
}
else {
}
?>
This is what I have but nothing is happening (I am getting no response)... :(
Thanks for your help!