I'm trying to make a simple login script and I've started with just trying to verify an email address. Essentially, from my login screen I am entering a valid email address and submitting it which should run AJAX to run a PHP function. I've scoured the internet all of last night and this morning trying different methods but I keep hitting dead ends.
To keep it brief here is the relevant code:
app.js
$(document).ready(function() {
$("submit").click(function() {
alert("This was a test.");
$.ajax({
type: "POST",
url: "main_login.php",
success: function(data) {
alert(data);
$(".message").text(data);
}
});
});
});
main_login.php
<?php
function checkLogin()
$conn = new mysqli('localhost', 'user', 'pass!', 'db');
mysqli_set_charset($conn, 'utf8mb4');
$check = "SELECT * FROM users WHERE email = '$_POST[email]'";
$results = mysqli_query($conn, $check);
$data = mysqli_fetch_array($results, MYSQLI_NUM);
if($data > 1) {
echo "Login successful";
} else {
echo "This account does not exist";
}
$conn->close();
}
?>
login.php
<div class="container login">
<div class="panel panel-default login-panel">
<div class="panel-heading">span class="log-h1">Sign-in</span>
</div>
<div class="panel-body">
<form name="loginform" action="login.php" method="post">
<div class="form-group">
<label for="username">Email</label>
<input type="email" class="form-control"
name="email" id="email" placeholder="Email address">
</div>
<div class="form-group" style="float: right;">
<input type="submit" class="btn btn-primary" name="Submit"
value="Sign-in">
</div>
</form>
<span class='message'></span>
</div>
</div>
</div>
I'm not getting anything happening. The page notifies me when I refresh it that it may resend data but when i submit my email it doesn't do anything.
mysqli_fetch_arrayto get your data from your recordset, but there's no error checking to make sure that your query has worked. Also, you can simplify your code -if ($row = mysqli_fetch_array($results))will return false if there are no matching resultsevent.preventDefault()for stop reload page