Jump to content


Photo

Errors on form validation


  • Please log in to reply
6 replies to this topic

#1 piercam03

piercam03
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 16 September 2006 - 03:28 PM

hi i have some code...

<?
// Website Contact Form Generator 
// http://www.tele-pro.co.uk/scripts/contact_form/ 
// This script is free to use as long as you  
// retain the credit link  

// get posted data into local variables
$EmailTo = "piercam03@gmail.com";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Message = Trim(stripslashes($_POST['Message'])); 
$Rating= Trim(stripslashes($_POST['Rating'])); 
$IP= $_SERVER["REMOTE_ADDR"];

// validation
$validationOK=true;

if (!$validationOK) {
  echo "Error";
  exit;
}


//THE ERRORS

if (Trim($Name)=="") {
  echo"The Name Filed Was Empty";
  exit;
}

elseif (Trim($Email)=="") {
  echo"The Email Field Was Empty";
  exit;
}

elseif (Trim($Message)=="") {
  echo"The Message Field Was Empty";
  exit;
}


// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $Subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "Rating: ";
$Body .= $Rating;
$Body .= "\n";
$Body .= "IP: ";
$Body .= $IP;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$Email>");

// redirect to success page 
if ($success){
  echo"<div id=\"main\"><h2>SUCCESS!</h2>Here is the information sent as you provided:<br>
Name: $Name <br>
Email: $Email <br>
Message: $Message <br>
Rating: $Rating <br>
Your IP address was also stored, IP: $IP <br><br>
Press F5 on your keyboard to return to where you were previously or select a page from the navigation bar on the left. <br><br>";
}
else{
  echo"<br><h2>ERROR</h2> Sorry, there was an error. <br><br> *NOTE* you MUST NOT click the contact page on the navigation bar or all the fields you filled in will be reset, If however you press the BACK button on your browser you will return to the form with your previous information saved.";
}

?>

basically when you fill out my form (www.iplay.co.nr/contact.php) its uses this file to send your details, the only problem is i can only get it to show one error at a time so even if the name and email fields are blank it only says the name one is, then once youve filled in the name one it tells you that the email is blank, how can i get it to show all of my errors together

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 16 September 2006 - 03:50 PM

//THE ERRORS

$errors=array();
if (Trim($Name)=="") {
  $errors[]="The Name Filed Was Empty";

}

elseif (Trim($Email)=="") {
  $errors[]="The Email Field Was Empty";
}

elseif (Trim($Message)=="") {
  $errors[]="The Message Field Was Empty";
}

if (!empty($errors)) {
 foreach ($errors as $error) {
  echo $error.'<Br>';
 }
 exit;
}

should do the trick :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 16 September 2006 - 03:51 PM

Change this:
//THE ERRORS

if (Trim($Name)=="") {
  echo"The Name Filed Was Empty";
  exit;
}

elseif (Trim($Email)=="") {
  echo"The Email Field Was Empty";
  exit;
}

elseif (Trim($Message)=="") {
  echo"The Message Field Was Empty";
  exit;
}

To this:
//THE ERRORS
$err="";
if ($Name=="") {$err.="The Name Filed Was Empty<br>";}
if ($Email=="") {$err.="The Email Field Was Empty<br>";}
if ($Message=="") {$err.="The Message Field Was Empty";}

if(!empty($err)){
die($err);
}

Orio.

EDIT- shocker-z is quicker =PP
Think you're smarty?

(Gone until 20 to November)

#4 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 16 September 2006 - 03:53 PM

$errors= array();

if ($errors) {
   print '<span class="rood"><ul><li><b>';
   print implode('</b></li><li><b>',$errors);
   print '</b></li></ul></span>';
   exit;
}

if (Trim($Name)=="") {
  $errors[] = "The Name Filed Was Empty";
}
elseif (Trim($Email)=="") {
  $errors[] = "The Email Field Was Empty";
}
elseif (Trim($Message)=="") {
  $errors[] = "The Message Field Was Empty";
}

RTFM is an almost extinct art form, it should be subsidized.

#5 piercam03

piercam03
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 16 September 2006 - 04:02 PM

im sorry to say but only orio's worked but thankyou every one else for trying woooooooo

#6 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 16 September 2006 - 04:35 PM

i had an exit; in one of the if's thats why but ive edited now and should work :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#7 piercam03

piercam03
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 16 September 2006 - 05:32 PM

cool




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users