I'm trying to make a response through my login.js
and my logindata.php
via my login.php
, however I always seem to get the error message Something went wrong! Try again.
Something went wrong! Try again.
(my own custom message in login.js
) even though I write in the correct login details
login.php
<form name="loginform" id="loginform" autocomplete="off">
<div class="form-group">
<input type="text" name="username" maxlength="12" class="form-control" placeholder="Username" id="username" required/>
</div>
<div class="form-group">
<input type="password" name="password" maxlength="12" class="form-control" placeholder="Password" id="password" required/>
</div>
<input id="login" type="submit" class="btn btn-primary btn-block" value="Login"/>
<a href="/register" class="btn btn-info btn-block">Register</a>
</form>
login.js
$(function () {
var action = '';
var form_data = '';
$('#login').click(function () {
action = $("#loginform").attr("action");
form_data = {
username: $("#username").val(),
password: $("#password").val(),
is_ajax: '1'
};
$('#login').keypress(function (e) {
if (e.which == 13) { //Enter key pressed
$('#login').click();
}
});
$.ajax({
type: 'POST',
url: 'logindata.php',
data: form_data,
success: function (response) {
if (response == 'success') {
$("#loginform").slideUp('slow', function () {
$("#message").html('<script>location.reload();</script><div class=\"alert alert-success\">Logged in. Reloading...</div>');
});
} else {
$('#message').hide().html("<br/><div class=\"alert alert-danger\">Something went wrong! Try again.</div>").fadeIn('fast');
}
console.log(response);
}
});
return false;
});
});
logindata.php
<?php
if(!isset($_SESSION['uid'])){
header("Location: /home")
}
if (!isset($_POST['submit'])){
header("Location: /home")
} else {
$mysqli = new mysqli("localhost","root","root","maplexeno");
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * from accounts WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
$_SESSION['uid'] = $username;
echo "success";
} else {
echo "false";
}
session_start();
}
?>
You're probably receiving an header output, or session_start()
error in your log. You have to put session_start()
at the very top of your logindata.php
script, right after <?php
and before anything else.
<?php
session_start();
// rest of your code...
You can try doing the following in login.js to see what PHP errors are being generated without having to go to your log file.
success: function (response) {
alert(response);
This should help you track down the issue if it's not session_start()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.