Jump to content

Registration form


graham23s

Recommended Posts

Hi Guys,

 

When i do my registration forms, i usually hit the user with an error if something is missing, but they then need to go back and fix this, by then the other details are wiped again and they need to put them all in again (same with javascript go back in this case)

 

i have seen a few sites that when there is an error , the input boxes are highlighted in red to show the user what went wrong, i'm wondering what would be the best way to do this?

 

reg.php

 

<?php

// titles array //
$titles = array("Dr","Doctor","Mr","Mister","Ms","Mrs","Miss");
      		               
// standard header //
print("<div class=\"subheader\"><div id=\"title\">Home > <span class=\"blue\">Register a new account</span></div>Please fill in all relevant information.</div>");

// check to see if the session exists //
if(isset($_SESSION['id']))
{
print("<div id=\"shopping_login_error\">You are already logged in.</div>");
include("inc/footer.php");
exit;
}

// grab the submitted data //
if(isset($_POST['submit']))
{

// make the vars //
$username = mysql_real_escape_string($_POST['username']);
$pass1 = mysql_real_escape_string(trim($_POST['password_1']));
$pass2 = mysql_real_escape_string(trim($_POST['password_2']));
$title = mysql_real_escape_string($_POST['title']);
$fname = mysql_real_escape_string(ucwords(strtolower($_POST['first_name'])));
$lname = mysql_real_escape_string(strtoupper($_POST['last_name']));
$address = mysql_real_escape_string(ucwords(strtolower($_POST['address'])));
$city = mysql_real_escape_string(ucwords(strtolower($_POST['city'])));
$state = mysql_real_escape_string(ucwords(strtolower($_POST['state_or_county'])));
$postzip = mysql_real_escape_string(ucwords(strtolower($_POST['post_zip_code'])));
$country = mysql_real_escape_string(strtoupper($_POST['country']));
$email = mysql_real_escape_string($_POST['email']);
$homenumber = mysql_real_escape_string($_POST['home_number']);
$mobnumber = mysql_real_escape_string($_POST['mobile_number']);
$newsletter = mysql_real_escape_string($_POST['news_letter']);
$specialoffers = mysql_real_escape_string($_POST['special_offers']);
$gender = mysql_real_escape_string($_POST['gender']);

// special optional value //
$optional = mysql_real_escape_string($_POST['which_products']);

// check for empty fields //
if(empty($username))
{
  display_error("You never entered a username.");
   //$errors[] = ("You never entered a username.");
}

if(empty($pass1))
{
  display_error("You never entered a password.");
   //$errors[] = ("You never entered a password.");
}

if(empty($pass2))
{
  display_error("You never confirmed your password.");
   //$errors[] = ("You never confirmed your password.");
}

if(empty($gender))
{
  display_error("You never selected your gender.");
   //$errors[] = ("You never selected your gender.");
}
  
if(empty($fname))
{
  display_error("You never entered your first name.");
   //$errors[] = ("You never entered your first name.");
}

if(empty($lname))
{
  display_error("You never entered your last name.");
   //$errors[] = ("You never entered your last name.");
}

if(empty($address))
{
  display_error("You never entered your address.");
   //$errors[] = ("You never entered your address.");
}

if(empty($city))
{
  display_error("You never entered your city.");
   //$errors[] = ("You never entered your city.");
}

if(empty($state))
{
  display_error("You never entered your state or county.");
   //$errors[] = ("You never entered your state or county.");
}

if(empty($postzip))
{
  display_error("You never entered your post or zip code.");
   //$errors[] = ("You never entered your post or zip code.");
}

if(empty($email))
{
  display_error("You never entered your e-mail address.");
   //$errors[] = ("You never entered your e-mail address.");
}

if(empty($homenumber))
{
  display_error("You never entered your home phone number.");
   //$errors[] = ("You never entered your home phone number.");
}
// make sure passwords match //
if($pass1 != $pass2)
{
  display_error("Your passwords don't match.");
   //$errors[] = ("Your passwords don't match."); 
}
// email in the proper format //
if(!(ereg ("^.+@.+\..+$", $email))) 
{ 
   display_error("Your e-mail looks invalid."); 
   //$errors[] = ("Your e-mail looks invalid."); 
}
// home number numeric //
if(!is_numeric($homenumber)) 
{
   display_error("Your home number should be numeric i.e. 01698123456."); 
   //$errors[] = ("Your home number should be numeric i.e. 01698123456.");  
}

// No errors INSERTION //

   // insertion //
   $query_registration = "INSERT INTO `fcp_customers` (`id`,`username`,`password`,`gender`,`title`,`first_name`,`last_name`,`address`,`city`,`state_county`,`postzipcode`,`country`,`email`,`homenumber`,`mobilenumber`,`newsletter`,`specialoffers`,`registered`) VALUES ('','$username','$pass1','$gender','$title','$fname','$lname','$address','$city','$state','$postzip','$country','$email','$homenumber','$mobnumber','$newsletter','$specialoffers',now())";

   // execute query //
   $result_registration = mysql_query($query_registration);
   
   if(!empty($optional))
   {  
   // also deal with the optional field //
   $query_optional = mysql_query("INSERT INTO `fcp_products_wanted` (`id`,`products`,`date`) VALUES ('','$optional',now())");  
   }
   
   // OK //
   if($result_registration)
   {   
   // SUCCESS //
   print("<br /><div class=\"formsuccess2\"><b>You have successfully registered. we have e-mailed you a copy of your login details please keep them safe. <img src=\"images/tick.jpg\"> </b></div>");
   include("inc/footer.php");
   exit; 
   
     // email string //
     $from = "admin@firstchoicepharmacy.co.uk";
     $to = $email;
     $subject = "FirstChoicePharmacy - Registration";
     $body = "Hi, <b>$fname</b><br /><br />";
     $body .="Thank you for registering an account with FirstChoicePharmacy.co.uk.<br /><br />";
     $body .="Your password is: <b>$pass1</b><br /><br />";
     $body .="Regards <a href=\"www.firstchoicepharmacy.co.uk\">www.firstchoicepharmacy.co.uk</a>";
     $headers = "MIME-Version: 1.0\r\n";  
     $headers = "From: FirstChoicePharmacy<$from>\n"; 
     $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
     
     // mail code //
     mail($to,$subject,$body,$headers);
       
   }
  
  } else {
     
  //}

  } // end isset //

// registration form //
print("<form action=\"register.php\" method=\"post\" name=\"loginform\" onSubmit=\"return logincheck()\">");
print("<table class=\"login_table\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">");
print("<tr>\n");
print("<td colspan=\"2\" class=\"login_border\" align=\"left\"><b>REGISTRATION</b></td>\n");
print("</tr>\n");
print("<td class=\"login_border\" align=\"right\">Select a username:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"username\" /></td>");
print("</tr>");
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Select a password:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"password_1\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Confirm your password:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"password_2\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Gender:</td><td class=\"login_border\" align=\"left\"><input type=\"radio\" name=\"gender\" value=\"Male\"><label>Male</label><input type=\"radio\" name=\"gender\" value=\"Female\"><label>Female</label></td>");
print("</tr>");  
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Title:</td><td class=\"login_border\" align=\"left\">");
print("<select name=\"title\">\n");
foreach($titles as $valtitle)
{
print("<option value=\"$valtitle\">$valtitle</option>\n");
}
print("</select>\n");
print("</td>");
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your first name:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"first_name\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your last name:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"last_name\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your address:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"address\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your city:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"city\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your postal / zip code:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"post_zip_code\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Select your country:</td><td class=\"login_border\" align=\"left\">");
print("<select name=\"country\">\n");
foreach($countrylist as $valcountry)
{
  print("<option value=\"$valcountry\">$valcountry</option>\n");
}
print("</select>\n"); 
print("</td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your state / county:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"state_or_county\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your e-mail address:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"email\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your home number:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"home_number\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Enter your mobile number:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"mobile_number\" /></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Signup for our newsletter:</td><td class=\"login_border\" align=\"left\"><input type=\"radio\" name=\"news_letter\" value=\"Y\"><label>Yes</label><input type=\"radio\" name=\"news_letter\" value=\"N\"><label>No</label></td>");
print("</tr>"); 
print("<tr>");
print("<td class=\"login_border\" align=\"right\">Signup for our special offers:</td><td class=\"login_border\" align=\"left\"><input type=\"radio\" name=\"special_offers\" value=\"Y\"><label>Yes</label><input type=\"radio\" name=\"special_offers\" value=\"N\"><label>No</label></td>");
print("</tr>");
print("<tr>");
print("<td class=\"login_border\" align=\"right\" valign=\"top\">Products you would like to see:<br />(Seperate with a space.)</td><td class=\"login_border\" align=\"left\"><textarea name=\"which_products\" rows=\"20\" cols=\"40\"></textarea></td>");
print("</tr>");
print("<tr>");
print("<td class=\"login_border\" colspan=\"2\" align=\"right\"><input type=\"reset\" name=\"reset\" value=\"Reset Form\"><input type=\"submit\" name=\"submit\" onClick=alert('Please make sure all fields are filled in correctly for a prompt delivery.'); value=\"Complete Registration\"></td>");
print("</tr>"); 
print("</table></form>");
?>

 

any tips would be great.

 

thanks guys

 

Graham

Link to comment
Share on other sites

its called AJAX (javascript and another language such as php or asp etc)

 

if you have never worked with AJAX before this will be a little difficult

but if you have then this will be simple.

 

heres a few nice tutorials on AJAX:

 

(this one has the php code too)

http://www.w3schools.com/ajax/ajax_source.asp

 

(you will have to make your own php code, but its not that difficult)

http://www.w3schools.com/ajax/ajax_database.asp

Link to comment
Share on other sites

think outside the box :)

 

Create an array of vars which are all the inputs

$allfields=array('name','pass','email','vpass','first_name','last_name','title');
[code]

create another array for required fields
[code]
$reqfields=array('name','pass','email','first_name','last_name');
[code]

Note, that vpass is not added as required since a matching check is gonna be used anyhow

process all fields
[code]
foreach($allfields as $field)
    $inputs[$field]=mysql_real_escape_string($_POST[$field]);

than check required fieldsa are filled :)

throw them into an error array, true false will work

foreach($reqfields as $field)
     $reqerrors[$field]=!empty($inputs[$field]);

 

now on yer form, ya can  display errors on the field

if(isset($reqfield['name']) && $reqerrors['name'])
   $color='BGCOLOR="RED"';  // Missing required field color
else
  $color='';  // use default color
print("<td class=\"login_border\" $color align=\"right\">Select a username:</td><td class=\"login_border\" align=\"left\"><input type=\"text\" size=\"40\" name=\"username\" /></td>");

 

note: color is inserted within the TD tag, u can also display an error as well

 

[/code][/code][/code][/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.