I am working on the following code using PHPStorm. I have three files:
* loginForm.php
(the html/php/javascript for logging in)
* firstlogin.php
(php code that send variables to public function)
* user.php
(The public function for checking the DB)
When the form is submitted, the page I am on reloads and that is it. Nothing changes.
Looking at the network tab in the inspector I see that the page calls firstlogin.php
the way it should. But it stops processing immediately and the debugger tab pops up because the processing was paused do to an error. It highlights line 3454 in jquery-3.3.1.min.js: s.send(t.hasContent && t.data || null)
. Upon continuing the code, nothing is returned, no headers or content. The browser just says "An error occurred trying to load the resource". If I click on the firstlogin.php
to view the content/headers/cookies/etc. Clicking on the reloaded loginform.php
gives me a 200 OK. I will include pictures and the code below. I don't know what to do.
I have tried:
- Changing the form to onsubmit(run the function)
Change submit input to button
Change the submit button to onclick(run the function)
Make jquery listen for click of the button
Make jquery listen for submit of the form (Current)
----Obviously these didn't really change anything but I had to try.
Change data type to JSON & serialize the form
I also tried advice from this page: Stackoverflow link
<!--loginform.php Javascript-->
$(function() {
$("#login-form").submit(function() {
if ($("#password").val() !== "") {
$.ajax({
method: "POST",
url: "<?=CALL_LOGINFILE?>",
data: {
password: $("#password").val()
},
}).done(function(msg) {
if (msg !== "") {
alert(msg);
} else {
window.location = "<?=CALL_REGISTER?>";
}
});
} else {
alert("Please fill all fields with valid data!");
}
});
});
<!--firstlogin.php Javascript-->
<?php
require __DIR__ . '/Components/Classes/user.php';
require __DIR__ . '/CallMeFirst.php';
$password = filter_input(INPUT_POST, 'password', FILTER_DEFAULT);
if( $user->loginPW($password) ) {
die;
} else {
$user->printMsg();
die;
}
?>
<!--user.php Javascript-->
<?php
public function loginPW($password){
if(is_null($this->pdo)){
$this->msg = 'Connection did not work out!';
return false;
} else {
$pdo = $this->pdo;
$stmt = $pdo->prepare('SELECT id, password FROM users WHERE id = 1 limit 1');
$stmt->execute();
$user = $stmt->fetch();
if(password_verify($password, $user['password'])){
$this->user = $user;
session_regenerate_id();
$_SESSION['user']['id'] = $user['id'];
return true;
} else {
$this->msg = 'Invalid login information';
return false;
}
}
}
?>
<!--loginform.php HTML-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.cookie@1.4.1/jquery.cookie.min.js"></script>
<form id="login-form" method="post" role="form" style="display: block;">
<div class="form-group">
<input type="password" name="password" id="password" tabindex="1" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<div class="form-row text-center justify-content-center">
<div class="col-md-6 col-md-offset-3">
<input type="button" name="login-submit" id="login-submit" tabindex="2" class="btn btn-login" value="SUBMIT">
</div>
</div>
</div>
</form>
header tab for firstlogin.php
header tab for reloaded login form.php
debugger tab