I’m trying to make a form to login users into my webpage using mysql db.
My page index.php has a form which posts to doLogin.php which checks user/pass from mysql database. Once a user is verified, I would like to show the username in a div to show they have been logged in.
index.php
JQuery
$document.ready(function() {
$("#login_b").click(function() {
var action = $("#form1").attr('action');
var form_data = {
username: $("#username").val(),
password: $("#password").val(),
is_ajax: 1
};
$.ajax({
type: "POST",
url: action,
data: form_data,
success: function(response)
{
if(response == 'success')
alert( username + "Te logeaste correctamente","alert window");
document.location.href='index_spa.php';
$("#user_data").html( username + " online");
if(response == 'empty')
alert("No hay datos para validar","alert window");
$("#user_data").html("Campos vacios");
document.location.href='index_spa.php'
if(response == 'incorrect')
alert("Error en los datos introducidos","alert window");
$("#user_data").html("Datos incorrectos");
document.location.href='index_spa.php'
if(response == 'inactive')
alert("Cuenta no activada","alert window");
$("#user_data").html("La cuenta no está activada");
document.location.href='index_spa.php'
}
});
return false;
});
});
HTML
<div id="user_data"></div>
<div id="login_box">
<form id="form1" action="doLogin.php" method="post">
<div id="border">
<table class="table_text" cellpadding="2" cellspacing="0" border="0">
<tr>
<td>Usuario:</td>
<td>
<input type="text" name="username" class="input" />
</td>
</tr>
<tr>
<td>Clave:</td>
<td>
<input type="password" name="password" class="input" />
</td>
</tr>
<tr>
<td colspan="2" align="right">
<a class="link_login" target="_new" href="register.php">Registrarse</a>
<input id="login_b" type="submit" name="submit" value="Login" class="button" />
</td>
</tr>
</table>
</div>
</form>
</div>
doLogin.php
<?php
//allow sessions to be passed so we can see if the user is logged in
session_start();
ob_start();
//connect to the database so we can check, edit, or insert data to our users table
$con = mysql_connect('localhost', 'winterfall', '14789632qwerty') or die(mysql_error());
$db = mysql_select_db('clientes', $con) or die(mysql_error());
//include out functions file giving us access to the protect() function made earlier
include "./functions.php";
//If the user has submitted the form
if($_POST['submit']){
//protect the posted value then store them to variables
$username = protect($_POST['username']);
$password = protect($_POST['password']);
//Check if the username or password boxes were not filled in
if(!$username || !$password){
//if not display an error message
echo "empty";
}else{
//if the were continue checking
//select all rows from the table where the username matches the one entered by the user
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
$num = mysql_num_rows($res);
//check if there was not a match
if($num == 0){
//if not display an error message
echo "empty";
}else{
//if there was a match continue checking
//select all rows where the username and password match the ones submitted by the user
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
$num = mysql_num_rows($res);
//check if there was not a match
if($num == 0){
//if not display error message
echo "incorrect";
}else{
//if there was continue checking
//split all fields fom the correct row into an associative array
$row = mysql_fetch_assoc($res);
//check to see if the user has not activated their account yet
if($row['active'] != 1){
//if not display error message
echo "no active";
}else{
//if they have log them in
//set the login session storing there id - we use this to see if they are logged in or not
$_SESSION['uid'] = $row['id'];
//show message
echo "success";
//update the online field to 50 seconds into the future
$time = date('U')+50;
mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
//redirect them to the usersonline page
//header('Location: usersOnline.php');
}
}
}
}
}
?>
please help me.
thank you
Sorry if this seems like an odd question, but did your form work as you expected with
doLogin.phpbefore adding the JQuery functionality or is this the first time that you have tried it?The main problem that I see with your JQuery is that you do not have open and closing brackets within your
ifstatements.So
Should be
You can also try using
alert( response )to see what you are getting back from the server.