Reputation: 53
<?php
$username=strip_tags($_POST['username']);
$password=strip_tags($_POST['password']);
$username=stripslashes($username);
$password=stripslashes($password);
$username=mysqli_real_escape_string($dbc,$username);
$password=mysqli_real_escape_string($dbc,$password);
$password=md5($password);
$sql="SELECT * FROM mobile_users WHERE username='$username' LIMIT 1 ";
$query=mysqli_query($dbc, $sql);
$row=mysqli_fetch_array($query);
//$expire=time() +60*60*24*30;
//setcookie("");
//}
$id=$row['idmobile_users'];
$db_password=$row['password'];
$_SESSION['error'] = "could not connect";
if($password==$db_password){
$_SESSION['username']=$username;
$_SESSION['id']=$id;
$_SESSION['last_time']=time();
header("Location: apply_now.php");
}
else {
echo 'Invalid username or password';
}
}
?>
<script>
$(function(){
$(document).on("click", ".button", function(e) {
e.preventDefault();
{
//This will capture all the inputs from the form
var infom = $("#myform").serialize();
//$("#showresult").addClass('loader');
$.ajax({
beforeSend: function() { },
type: "POST",
url: "login.php",
data:infom,
success: function(result){
//$("#showresult").removeClass('loader');
$('#showresult').html(result);
}
});
e.preventDefault();
}
});
});
</script>
ok so i want it to place the error message from the php code where the id is in html, the id is in a span tag. Tried alot of ways but cant seem to get it working properly. I hope somone can help me resolve this issue. Thanks
Upvotes: 1
Views: 695
Reputation: 2879
First, Ajax code will not understand if the response from php page if it is an error or a success response, What you do just return the response but specify if it is an error or not in the PHP page.
It would be better if you separate your php page from your html page.
This should be in your html page.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.js"></script>
<script>
$(function(){
$(document).on("click", ".button", function(e) {
e.preventDefault();
{
//This will capture all the inputs from the form
var infom = $("#myform").serialize();
$("#showresult").addClass('loader');
$.ajax({
beforeSend: function() { },
type: "POST",
url: "login.php",
data:infom,
success: function(result){
$("#showresult").removeClass('loader');
$('#showresult').html(result);
}
});
e.preventDefault();
}
});
});
</script>
<style>
.loader{
background-image:url(http://loading.io/assets/img/default-loader.gif);
background-repeat:no-repeat;
background-position:center;
height:100px;
background-size: 30px;
}
</style>
<div id="showresult"></div>
<form id="myform">
<!--your inputs-->
<input name="username" type="text"><br>
<input name="password" type="text"><br>
<button class="button"> submit </button>
</form>
And this on your php page
<?php
//YOUR PHP CODES....
header("Location: apply_now.php");
// i would prefer something like
echo ' welcome '.$username.' ';
echo "<script> window.location.href='apply_now.php';</script>";
}
else {
echo 'Invalid username or password';
}
echo json_encode(array('error' => $error, 'message' => $message, 'redirect' => $redirect));
?>
Upvotes: 1
Reputation: 79
I dont understand exactly what you mean but when i look at the code could it be that youre page is just refreshing afther the submit? If im correct the form will reload the page on a submit if you dont use e.preventDefault(). If it is reloading just include the prevent default, the rest of youre code looks ok
Upvotes: 0