I'm trying to make a login form using Ajax and PHP backend. However, when I click on the submit button, nothing happens other than the modal box closing. I did some testing and it looks like the $("#login").click(function(){ does not even get called. Below is my code. Why is this happening?
Here's my JS code:
$(document).ready(function(){
// Open login modal when login button is clicked
$("#login_btn").click(function(){
$('#login_modal').modal();
});
// Submit the login information when the sign in button is clicked
$("#login").click(function(){
var user=$("#user").val();
var password=$("#password").val();
var login_info="user="+user+"&password="+password;
$.ajax({
type: "POST",
url: "../php/authentication.php",
data: login_info,
success: function(msg){
var msg=trim(msg);
// Redirect user if authentication is successful
if(msg == 'OK') {
window.location = '../../action-log.php';
}
// Print error message
else {
$('#login_response').html(msg);
}
}
});
});
});
Here's the code for the form:
<link type="text/css" rel="stylesheet" media="screen" href="assets/css/css_action-log-login.css">
<script type="text/javascript" charset="utf-8" src="./assets/js/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="./assets/js/jquery.simplemodal.js"></script>
<script type="text/javascript" charset="utf-8" src="./assets/js/js_action-log-login.js"></script>
<div id="login_modal" style="display: none;">
<div class="heading">Sign In</div>
<div id="login_response"></div>
<form id="login_form" method="post">
<table align="center" style="margin-top: 25px;" border="0">
<tr><td>
<label>Username: </label><br/><br/>
<label>Password: </label>
</td><td>
<input type="text" name="user" id="user" /><br/><br/>
<input type="password" id="password" name="password" />
</td></tr>
<tr><td></td><td align="left">
<input type="submit" name="login" id="login" value="Sign In" class="button blue" />
</td></tr>
</table>
</form>
</div>
Here's authentication.php:
<?php
// Requires
require('./config.inc.php');
require('./functions.inc.php');
// Session
session_start();
// Error messages
$errormsg=array();
// Check if the username field is empty
if(isset($_POST['user']) && !empty($_POST['user'])){
$user=mysql_real_escape_string($_POST['user']);
} else {
$errormsg[]='username';
}
// Check if the password field is empty
if(isset($_POST['password']) && !empty($_POST['password'])){
$password=mysql_real_escape_string($_POST['password']);
} else {
$errormsg[]='password';
}
// Print error message
if(count($errormsg) > 0) {
echo 'Please enter your ';
for($i=0; $i < count($errormsg); $i++){
echo $errormsg[$i];
if($i < count($errormsg)-2) echo ', ';
if($i == count($errormsg)-2) echo ' and ';
}
}
// Verify authentication details
else {
$db = new mysql();
$salt = "*65'][as'``'fg4";
$querySQL = ('
SELECT COUNT(*)
FROM al_users
WHERE username = "'.$user.'"
AND password = PASSWORD("'.$password.$salt.'")
');
$result = mysql_query($querySQL);
if(!$result) die(mysql_error());
$row = mysql_fetch_row($result);
if($row[0] == 0) {
echo 'Invalid email or password.';
} else {
$_SESSION['name'] = $user;
echo 'OK';
}
}
?>
$('#login_form').submit()instead of$('#login').click()