Jump to content

Errors on form validation


piercam03

Recommended Posts

hi i have some code...

[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 = "[email protected]";
$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.";
}

?>[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/20979-errors-on-form-validation/
Share on other sites

[code]
//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;
}
[/code]

should do the trick :)
Change this:
[code]//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;
}[/code]

To this:
[code]//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);
}[/code]

Orio.

EDIT- shocker-z is quicker =PP
[code]
$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";
}[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.