- Joined
- Mar 21, 2009
- Messages
- 6,546 (1.11/day)
- Location
- Midlands,UK
System Name | Reborn|Partner |
---|---|
Processor | AMD Ryzen 9600x|AMD Ryzen 7 5700x |
Motherboard | Asrock B650m Pro-RS|Asus B550m Prime Wifi |
Cooling | Arctic cooling freezer 34 white + Montech 120mm | Thermalright Silversoul 135 + Corsair 140mm white |
Memory | Corsair Vengeance DDR5 32GB 6000mhz|Corsair Vengeance DDR4 16GB 3200mhz |
Video Card(s) | XFX AMD RX 9070XT Swift Black 16GB| Zotac Nvidia RTX 4080 Super Trinity black edition 16GB |
Storage | 2x2TB WD Black SN1700|WD Blue 500GB NVME,Kingston 240GB SSD,2TB Corsair NVME |
Display(s) | AOC Gaming CU34G3S/BK | Iiyama XUB2792QSU gen 2 + Iiyama XUB2792QSU gen 1 |
Case | Montech Air 100 White| Corsair Carbide Air 240 White |
Audio Device(s) | onboard/Speakers: Logitech G560 | onboard/Speakers: Sanyun SW208 white 2.0 |
Power Supply | Seasonic Focus GX 850w Gold Modular | Corsair RMx White 750w Gold Modular |
Mouse | Microsoft Pro Intellimouse shadow | Razer Deathadder Essential white |
Keyboard | Epomaker mechanical keyboard| Razer huntsman v2 |
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');
?>