PDA

Click to See Complete Forum and Search --> : client side error message in php login.php


nebrom
Feb 12th, 2009, 09:08 AM
i have a login.php page.

<html>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table></body>
</html>
checklogin.php is as follow<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$username=$_POST['username'];
$password=md5($_POST['password']);

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row

if($count==1){
// Register $username, $password and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
this all is used to log in and if there is error it sends error message,
but now i want to send the error message from the client side, i want to see the errors along with login.php side by side.
for example, if there is error with username, along side to the username textbox, the error must appear, so how do i do that? may so eay for someone while it is so difficult for me. thanks

ngreenwood6
Feb 12th, 2009, 09:19 AM
Not trying to be rude or anything but maybe you should only post once, get that resolved and then move onto other posts. You may want to focus on one thing at a time so that you can fully understand that instead of trying to do five different things, makes it much harder to learn.

Anyways, javascript is the best option when you are trying to do something like that but you can also do it with php. Basically instead of having two separate forms you would just have on that authenticated to itself. When you have an error you can make it show up and when you dont you can make it not if that makes any sense. Look into posting a form to itself and then it will be easier to show you from there.

kows
Feb 12th, 2009, 05:45 PM
first, like mentioned above, you need to combine it into the same script. I did it myself below, and made a quick modification that will show the errors above the form if you submit the form and get an error. also, make sure this is "login.php," as it is posting to "login.php" unless you change it.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
//the form was submitted.


$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$username=$_POST['username'];
$password=md5($_POST['password']);

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row

if($count==1){
// Register $username, $password and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:login_success.php");
}else{
$errors = "<h1 style='color:red'>Wrong Username or Password</h1>";
}



} //end post if

//show the form
?>
<html>
<body>
<?php
//if there are errors, show them now
if($errors){
echo $errors;
}
?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="login.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table></body>
</html>
also, as an aside: you can register a session variable by defining the variable in the $_SESSION array. using the session_register() function is deprecated and the ability to do so will be removed in PHP6. you will also need to use session_start() to initiate or resume a session before you can register the variables now, because session_register() would do that automatically before. so, you can put this code below where you're currently calling session_register():
session_start();
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
and yeah, I'd stick to asking a question in one thread rather than starting three threads dealing with basically the same problems.