How to display the error of missing field under the specifc field
03-16-2006, 08:20 AM
|
How to display the error of missing field under the specifc field
|
Posts: 145
|
Hello everyone,
I have created a sign up form where the user fill in 13 fields in which 5 of them are required. The required fields are the following :forename,surname,username,password,confirm password.I have manage to check if the user has enter details in the fields using the function strlen( ). For the password and confirm password i am also checking if these two matches and at the username i also check if the username alreday exist in the database. The problem is that if the user has either forgot to put data or already exist or missmatch the password it displays the same error for the password and also erase all the other fields which has already been filled. What i would like help with is to manage and display the correct error message under the field that has the error and also to keep the data to the fields that has the correct details.
I have post also the code i have already write.I would appreciate your help.
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?
include "dbconn.php";
$title=$_POST['title']; $forename=$_POST['forename']; $surname=$_POST['surname']; $username= $_POST['username']; $pass=sha1($_POST['password']); $confirm=$_POST['confirm']; $email=$_POST['email']; $address1=$_POST['address1']; $address2=$_POST['address2']; $fulladdress="$address1 "." $address2"; $town=$_POST['town']; $county=$_POST['county']; $postcode=$_POST['postcode']; ?>
<html> <head> <title>Confirm User sign up</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> <!-- .error { font-family: "Times New Roman", Times, serif; color: #99FF33; } body,td,th { font-family: Arial, Helvetica, sans-serif; color: #009900; } --> </style> </head>
<body> <? if (strlen($forename)<0) { echo "no forename"; }
elseif (!$pass==$confirm){ echo "no match password";}
elseif (strlen($surname)<0) { echo "no surname"; }
elseif (strlen($username)<0) { echo "no surname"; }
elseif ($username==$row['username']) { echo "this username already exist,choose another"; }
else{ //echo "The password match the confirm password"; $query="Insert into Customers (`title`,`username`, `password`, `firstname`, `lastname`, `address`, `town`, `county`, `postcode`, `email`) values ('$title','$username','$pass','$forename','$surname','$fulladdress','$town','$county','$postcode','$email')"; $rs=mysql_query($query); if ($rs){ echo $forename ."thank you for signing in the website ";} else { echo "An error occured";} } ?> <input name="title" type="hidden" value="<? echo $title;?>"> <input name="forename" type="hidden" value="<? echo $forename;?>"> <input name="surname" type="hidden" value="<? echo $surname;?>"> <input name="username" type="hidden" value="<? echo $username;?>"> <input name="password" type="hidden" value="<? echo $pass;?>"> <input name="confirm" type="hidden" value="<? echo $confirm;?>"> <input name="email" type="hidden" value="<? echo $email;?>"> <input name="address1" type="hidden" value="<? echo $address1;?>"> <input name="address2" type="hidden" value="<? echo $address2;?>"> <input name="town" type="hidden" value="<? echo $town;?>"> <input name="county" type="hidden" value="<? echo $county;?>"> <input name="postcode" type="hidden" value="<? echo $postcode;?>"> </body> </html>
Thank you in advance,
Xenia
|
|
|
|
03-16-2006, 01:23 PM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 1,832
Location: Somewhere else entirely
|
Can we see the code for the orignal form that submits the data (the inputs in the script you posted are hidden ones)
You'll need to display the error messages in the middle of the form, so you need to add come code in between each <input>, which will do nothing if everything is OK, but will print out the error if any of the checks have failed.
There are some other problems which migh cause difficulty too:
PHP Code:
if (strlen($forename)<0)
^ The string length can never be less than zero, so this check will never fail (ie it will never do anything at all). I suggest you change this to be strlen($whatever)==0 or strlen($whatever) < 1.
PHP Code:
elseif (!$pass==$confirm){
^ This one's tricky. It evaluates as if you had written (!$pass) == $confirm (which will always fail since it' like writing false == true). To compare the strings, write !($pass == $confirm), or instead $pass != $confirm, which will fail if the strings are not equal.
PHP Code:
elseif ($username==$row['username'])
^ I can't see where $row gets set in this script?
Also the hidden inputs are floating on their own, they need to be wrapped in a <form> of their own or else they won't work.
With forms I usually write the whole thing on one page like this:
PHP Code:
<?php
function display_form($errors) { echo "<form action='whatever' method='POST'>"; echo "<input type='text' name='username' value='".$_POST['username']."' />"; echo $errors['username']; echo "<input type='text' name='surname' value='".$_POST['surname']."' />"; echo $errors['surname']; //other form fields as you need them... echo "</form>" }
function check_data(&$errors) { //set up a blank set of errors $errors = Array(); $ok = true;
if(strlen($_POST['username']<1)) { //If something fails, fill in the erros and set $ok to false $errors['username'] = "Username must be set"; $ok = false; } if($_POST['password'] != $_POST['confirm']) { $errors['password'] = "Password must match confirmation"; $ok = false; } // Other checks here
//If everything checked out, $ok is still true return $ok; }
function process_form() { //Do stuff like database insertions here
}
//Check if the form was submitted if(isset($_POST['submit'])) { $errors = Array(); if( !check_form($errors)) { display_form($errors); } else { process_form(); } } else { display_form(Array()); }
?>
Ooops, gotta run... let me know if you want a further explanation of any of this....
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
|
|
|
|
03-17-2006, 04:03 AM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 145
|
Hi 0beron,
Thank you for your answer. The error checking i have to put in the page that displays the form?I have also post the page with the code of the form as you asked me. I have created the form signup.php that has 13 fields (5 required) that the user should fill in in order to sign up.When the user submit the form the action goes to the page i displayed at my first thread called confirmsignup.php which check for the correct values and then insert in the database.
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>A Brief Encounter - Sign up</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
}
body {
background-color: #000000;
}
.style1 {color: #FFFFFF}
.style3 {color: #FF0000}
.style4 {color: #000000}
.style5 {font-size: 10px}
-->
</style></head>
<body>
<table width="782" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="782" height="40" valign="top"><div style="position:absolute; left: 577px; top: 329px;" class="style3"><span class="style3"><span class="style4"><span class="style5">required fields *</span></span></span></div></td>
</tr>
<tr>
<td height="18" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td height="27" valign="top">
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
<!--DWLayoutTable-->
<tr>
<fieldset ><legend class="style1" >Please fill in your details for singing up to our website </legend>
<td width="782" height="254" valign="top" bgcolor="#999999">
<div style="position:absolute ">
<form action="confirmsignup.php" method="post" enctype="multipart/form-data" name="signup">
<div style="position:absolute; left: 57px; top: 13px;">
<label>Title: </label></div>
<div style="position:absolute; left: 208px; top: 13px; ">
<select name="title">
<option value="1">Ms</option>
<option value="2">Mrs</option>
<option value="3">Miss</option>
<option value="4">Mr</option>
<option value="5">Dr</option>
<option value="6">Prof</option>
</select></div>
<br>
<div style="position:absolute; left: 57px; top: 46px;">
<label>Forename*:</label></div>
<div style="position:absolute; left: 208px; top: 46px;"><input name="forename" type="text" id="forename" ></div>
<div style="position:absolute; left: 57px; top: 77px; ">
<label>Surname*:</label></div>
<div style="position:absolute; left: 208px; top: 78px;"><input name="surname" type="text" id="surname" ></div>
<div style="position:absolute; left: 57px; top: 107px;">
<label>Username*:</label></div>
<div style="position:absolute; left: 208px; top: 107px;"><input name="username" type="text" id="username" ></div>
<div style="position:absolute; left: 57px; top: 138px;">
<label>Password*: </label></div>
<div style="position:absolute; left: 208px; top: 136px;"><input name="password" type="password" id="password" ></div>
<div style="position:absolute; left: 57px; top: 173px; width: 146px;">
<label>Confirm password*:</label></div>
<div style="position:absolute; left: 208px; top: 169px;"><input name="confirm" type="text" ></div>
<div style="position:absolute; left: 57px; top: 202px;">
<label>Email: </label></div>
<div style="position:absolute; left: 208px; top: 197px;"><input name="email" type="text" id="email" ></div>
<div style="position:absolute; left: 421px; top: 14px;"><label>Address: </label></div>
<div style="position:absolute; left: 495px; top: 13px;"><input name="address1" type="text" id="address1" ></div>
<div style="position:absolute; left: 495px; top: 45px;"><input name="address2" type="text" id="address2" ></div>
<div style="position:absolute; left: 421px; top: 87px;"><label>Town/City:</label></div>
<div style="position:absolute; left: 495px; top: 87px;"><input name="town" type="text" id="town" ></div>
<div style="position:absolute; left: 421px; top: 137px;"><label>County: </label></div>
<div style="position:absolute; left: 495px; top: 136px;"><input name="county" type="text" id="county" ></div>
<div style="position:absolute; left: 421px; top: 181px;"> <label>Postcode: </label></div>
<div style="position:absolute; left: 495px; top: 180px;"> <input name="postcode" type="text" id="postcode" ></div>
<div style="position:absolute; left: 496px; top: 226px;"><input name="submit" type="submit" value="Sign Up"></div>
</form>
</div>
<p class="style3"> </p>
<p class="style3"> </p>
</td></fieldset>
</tr>
</table>
<table width="782" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="554" height="57" valign="top"><!--DWLayoutEmptyCell--> </td>
<td width="228" valign="top"> <img src="images/site_home0002.jpg" width="220" height="55"></td>
</tr>
</table>
</body>
</html>
Could please suggest the best way to do it?
Thank you in advance,
Xenia
|
|
|
|
03-17-2006, 03:04 PM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 1,832
Location: Somewhere else entirely
|
My suggestion is to put the form handling into one page, and I've outlined how to do this in the post I made above.
Another question: are you building these pages in dreamweaver, or coding them by hand? If you are using dreamweaver, I canmove this thread over to the WYSIWYG editors forum which deals with Dreamweaver/MS Frontpage etc.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
|
|
|
|
03-20-2006, 06:25 AM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 145
|
Yes 0beron I am using Dreamweaver. I am also interesting to write the login in order to be secure.
Actually I have again problem with the form and specific the encrypted password.When the user tryies to log in it doen't let him enter even if the username and password are correct.
|
|
|
|
03-20-2006, 06:48 AM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 145
|
Actually the dynamic part of the website I write the code, I am not using the wizard of dreamweaver.
Regarding the error with the login i mention before I will post the code
here if you can see any error to help me
PHP Code:
<? include "dbconn.php";
$username=$_POST['username']; $pass=$_POST['password']; $encrypted_pass=sha1($_POST['password']); ?>
</head>
<body > <? $query = "SELECT * FROM Customers WHERE username= '$username' and password='$encrypted_pass'"; $result = mysql_query($query); $row = mysql_fetch_array($result); //echo "line"; echo $query; echo $row['username']; echo $row['password']; echo $row['address']; //$encrypted_password = sha1($_POST['password']); if ($username!=$row['username']) { echo "<h3>Your username is not correct. Please try again!</h3>"; include("userlogin.php"); }else if($encrypted_password!=$row['password']) { echo "<h3>Your password is not correct. Please log on again!</h3>"; include("userlogin.php"); }else{ //$_SESSION['username']=$username; include("index.php"); } ?> <? echo $username; ?> </body> </html>
The user write the username and password in another page called login.php and is being redirected to the confirm.php which i post above.
At first it was working i don't know what happen suddendly.I don't remember to change anything.
Thank you in advance,
Xenia
|
|
|
|
03-20-2006, 01:41 PM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 1,832
Location: Somewhere else entirely
|
You've commented out the line //$_SESSION['username']=$username;,
so if index.php checks for an active session it will never find one.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
|
|
|
|
03-21-2006, 05:05 AM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 145
|
Yes, you were right thank you.If I want to display in every page in my website the name of the user do i have to use the expression <? session_start(); ?> to every page?For example when the user wants to do checkout and buy the items I want to check is the user has already been login otherwise to redirect him/her to the login page.
|
|
|
|
03-21-2006, 03:24 PM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 1,832
Location: Somewhere else entirely
|
Every page you want to use sessions with must call session_start(). Usually this is done by putting all your logic for user login into a short script which you include() at the top of the other pages where you need sessions. Something like
PHP Code:
<?php session_start();
if($_SERVER['PHP_SELF'] == "name_of_your_logout_page") { session_destroy(); }
if(!isset($_SESSION['username'])) { header("location:http://www.yoursite.com/you_are_not_logged_in.php"); }
This is just an example of what you might do - it may differ depending on how the rest of your site is structured and how secure you want to be.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
|
|
|
|
03-22-2006, 04:40 AM
|
Re: How to display the error of missing field under the specifc field
|
Posts: 145
|
Thank you Oberon,I would like to ask you again regarding the missing fields .
I tryied to put all the code in one page as you suggest me but i have a problem as when the user submit the form it displays all the error message instead of the error message for the specific field.
I will post my code and i would appreciate your help.
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<? if (isset($_POST['submit'])) {//If submit button has been clicked include("dbconn.php"); //First check if there is data and is valid //check for the forename if (eregi ("^[[:alpha:].' -] {2,15}$", stripslashes(trim($_POST['forename'])))) { $forename=escape_data($_POST['forename']); }else { $forename= FALSE; echo '<h3 class="error">Please enter your forename!</h3>'; } //check for the surename if (eregi("^[[:alpha:].' -] {2,30}$", stripslashes(trim($_POST['surname'])))) { $surname=escape_data ($_POST['surname']); }else { $surname= FALSE; echo '<h3 class="error"> Please enter your surname! </h3>'; } //check for the username if (eregi("^[[ :alnum:].' -] {4,20}$", stripslashes(trim($_POST['username'])))) { $username=escape_data ($_POST['username']); }else { $username= FALSE; echo "<h3 class=\"error\"> Please enter a valid username! </h3>"; } //check for the password if (eregi("^[[ :alnum:].' -] {4,8}$", stripslashes(trim($_POST['password'])))) { if ($_POST['password']==$_POST['confirm']) &nb | |