- Joined
- Mar 21, 2009
- Messages
- 6,163 (1.13/day)
- Location
- Midlands,UK
System Name | Ultraman |
---|---|
Processor | Intel core i9 13900HK |
Motherboard | Alienware/Dell inverted motherboard |
Cooling | Alienware/Dell cooling solution + IETS GT500 cooling pad |
Memory | Alienware/Dell 32GB 6000mhz DDR5 |
Video Card(s) | Nvidia RTX4080 12GB |
Storage | 1TB Dell supplied NVME + Samsung Evo 1TB NVME |
Display(s) | AOC Gaming CU34G3S/BK |
Case | Alienware X16 |
Audio Device(s) | onboard/Speakers: Logitech G560 |
Power Supply | Alienware/Dell battery + 330W charger |
Mouse | Microsoft Pro Intellimouse - White |
Keyboard | Ducky One2 SF White MX cherry Red |
Software | Windows 11 |
Hey guys i need some help with this login page of mine which implements the use of Ajax.
I have followed this tutorial:
http://www.91weblessons.com/php-ajax-login-validation-tutorial/
Now assuming if i have declared my variables correctly because when i enter the login details it echos "correct" but if i didn't it echo "enter correct details" or leave the text blank it ask me to fill the spaces.
The issue is its not parsing through to the index.php script correctly,i have to refresh the page for it to take me to the index page.
Any help or advice be great here's what my codes look like:
login.php:
I have followed this tutorial:
http://www.91weblessons.com/php-ajax-login-validation-tutorial/
Now assuming if i have declared my variables correctly because when i enter the login details it echos "correct" but if i didn't it echo "enter correct details" or leave the text blank it ask me to fill the spaces.
The issue is its not parsing through to the index.php script correctly,i have to refresh the page for it to take me to the index page.
Any help or advice be great here's what my codes look like:
login.php:
processed.php:<?php
session_start();
if(isset($_SESSION['LOGIN_STATUS']) && !empty($_SESSION['LOGIN_STATUS'])){
header('location:index.php');
}
?>
<head>
<title>PHP Ajax Login Validation Tutorial | 91 Web Lessons</title>
<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
function validLogin(){
var username=$('#username').val();
var pwd=$('#pwd').val();
var dataString = 'username='+ username + '&pwd='+ pwd;
$.ajax({
type: "POST",
url: "processed.php",
data: dataString,
cache: false,
success: function(result){
var result=trim(result);
$("#flash").hide();
if(result=='correct'){
window.location='list.php';
}
else{
$("#errorMessage").html(result);
}
}
});
}
function trim(str){
var str=str.replace(/^\s+|\s+$/,'');
return str;
}
</script>
</head>
<body>
<div id="wrapper">
<table align="center" class="login_box">
<tr><td colspan="2" id="errorMessage"></td></tr>
<tr>
<td>UserName</td>
<td><input type="text" name="username" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" id="pwd"></td>
</tr>
<tr id="button_box">
<td> </td>
<td><input type="button" name="submit" value="Submit" class="button" onclick="validLogin()"></td>
</tr>
<tr><td colspan="2" id="flash"></td></tr>
</table>
</div>
</body>
</html>
index.php:<?php
session_start();
include_once('johndb.php');
$message=array();
if(isset($_POST['username']) && !empty($_POST['username'])){
$username=mysql_real_escape_string($_POST['username']);
}else{
$message[]='Please enter username';
}
if(isset($_POST['pwd']) && !empty($_POST['pwd'])){
$pwd=mysql_real_escape_string($_POST['pwd']);
}else{
$message[]='Please enter password';
}
$countError=count($message);
if($countError > 0){
for($i=0;$i<$countError;$i++){
echo ucwords($message[$i]).'<br/><br/>';
}
}else{
$query="select * from signup where username='$username' and pwd='$pwd'";
$result=mysql_query($query);
$checkUser=mysql_num_rows($result);
if($checkUser > 0){
$_SESSION['LOGIN_STATUS']=true;
$_SESSION['username']=$username;
echo 'correct';
}else
{
echo ucwords('please enter correct user details');
}
}
?>
logout.php:<?php
session_start();
if(!isset($_SESSION['LOGIN_STATUS'])){
header('location:login.php');
}
?>
<html>
<head>
<title>PHP Ajax Login Validation Tutorial | 91 Web Lessons</title>
</head>
<body>
<div id="container">
<!--top section start-->
<div id='tutorialHead'>
<div class="tutorialLink"><a href="list.php" title="List"><h1>List</h1></a></div>
<div class="logout"><a href="logout.php" title="logout"><h1>Logout</h1></a></div>
</div>
<div id="wrapper">
<div class="user_intro"><h1>Welcome <?php echo $_SESSION['username'];?></h1></div>
</div>
</body>
</html>
<?php
session_start();
session_destroy();
header('location:login.php');
?>