I'm having a HTML page where you can enter a password to be redirected to an intern page called "Files.php". If the entered password is wrong, I give a feedback in my form, if it is correct, I want to redirect to the intern page.
My HTML looks like this:
<form name="download" id="download">
<label for="password">Passwort</label><br>
<div id="wrongpassword"></div>
<input type="password" id="password" name="password" required> <br>
<input type="submit" id="submit" value="Download">
</form>
My AJAX request like this:
$(document).ready(function () {
var request;
$("#download").submit(function(event){
event.preventDefault();
var $form = $(this);
var $inputs = $form.find("input, select, button, textarea");
var serializedData = $form.serialize();
$inputs.prop("disabled", true);
request= $.ajax({
url: "download.php",
type: "post",
data: serializedData
});
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
if(response=="Success"){
window.location.replace("Files.php");
}else{
$("#wrongpassword").html(response);
} });});
And my PHP like this:
<?php
session_start();
$db = new PDO('mysql:host=localhost;dbname=nachhilfe', 'root', 'secret');
/*if(!$db)
{
exit("Verbindungsfehler: ".mysqli_connect_error());
}
else{
echo "Success!";
}*/
$pw = ($_POST['password']);
$statement = $db->prepare("SELECT passwort FROM downloads WHERE id= :id");
$result = $statement->execute(array(':id' => 4));
$stored = $statement->fetch();
if(password_verify($pw, $stored['passwort'])){
$_SESSION['verified'] = true;
echo "Success";
}
else{
echo "Wrong password";
}?>
So far, everything is working, except from the response compare if(response=="Success")
, which is always false.
How can i compare my AJAX response or is there a nicer way to achieve the redirect?