0

Right now the errors are showing up separately, I want to make it so that they show up right over the contact form? How can this be done? I am assuming I will have to edit the died($error) function. Any help will be appreciated.

    if(isset($_POST['Email_Address'])) {

    include 'freecontactformsettings.php';

    function died($error) {
        echo "Sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['Full_Name']) ||
        !isset($_POST['Email_Address']) ||
        !isset($_POST['Website']) ||
        !isset($_POST['Your_Message']) || 
        !isset($_POST['AntiSpam'])      
        ) {
        died('Sorry, there appears to be a problem with your form submission.');        
    }

    $full_name = $_POST['Full_Name']; // required
    $email_from = $_POST['Email_Address']; // required
    $website = $_POST['Website']; // not required
    $comments = $_POST['Your_Message']; // required
    $antispam = $_POST['AntiSpam']; // required

    $error_message = "";

  if(strlen($full_name) < 2) {
    $error_message .= 'Your Name does not appear to be valid.<br />';
  }
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(preg_match($email_exp,$email_from)==0) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $website_exp = '/\b(?:(/>/>?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
  if (!empty($website) && !preg_match($website_exp, $website)) {
    $error_message .= 'The Website you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if($antispam <> $antispam_answer) {
    $error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\r\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Full Name: ".clean_string($full_name)."\r\n";
    $email_message .= "Email: ".clean_string($email_from)."\r\n";
    $email_message .= "Website: ".clean_string($website)."\r\n";
    $email_message .= "Message: ".clean_string($comments)."\r\n";

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
header("Location: $thankyou");
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}

<form method="post" name="freecontactform" id="freecontactform" onsubmit="return validate.check(this)">
      <table width="550" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="top" class="formdesc">Name:</td>
        </tr>
        <tr>
          <td valign="top" class="formtd"><input name="Full_Name" type="text" class="formtxt3" id="Full_Name" /></td>
        </tr>
        <tr>
          <td valign="top" class="formdesc">Email Address:</td>
        </tr>
        <tr>
          <td valign="top" class="formtd"><input name="Email_Address" type="text" class="formtxt3" id="Email_Address" maxlength="100" /></td>
        </tr>
        <tr>
          <td valign="top" class="formdesc">Website:</td>
        </tr>
        <tr>
          <td valign="top" class="formtd"><input name="Website" type="text" class="formtxt3" id="Website" maxlength="100" /></td>
        </tr>
        <tr>
          <td valign="top" class="formdesc">Message:</td>
        </tr>
        <tr>
          <td valign="top" class="formtd"><textarea name="Your_Message" class="formtxt3" id="Your_Message" style="height:160px"></textarea></td>
        </tr>
        <tr>
            <div class="otherbutton"><a href="#" onclick="document.forms['freecontactform'].submit();">SUBMIT</a></div></td>
        </tr>
      </table>
    </form>
  • 2
    Regarding your [email address validation](https://emailtester.pieterhordijk.com/test-pattern/MzA) and how to [fix it](http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php/12026863#12026863) – PeeHaa Feb 10 '14 at 01:12
  • if you want the error messages to show up elsewhere, then you'll have to move your error message output logic. It's not exactly rocket science... – Marc B Feb 10 '14 at 01:21

2 Answers2

0

You'll have to echo the errors above the form.

Ryan Printup
  • 456
  • 3
  • 9
  • Only problem is that there are different errors for different inputs being wrong –  Feb 10 '14 at 01:42
0

Use an array to record the errors. If there is an error, output that according to the keys.

László Papp
  • 51,870
  • 39
  • 111
  • 135
define.cc
  • 71
  • 6
  • Using array to display to display the error would solve the error problem but how will we tell the form to stop from submitting? –  Feb 10 '14 at 05:27
  • you could use javascript to do the validation before submit rather than php – define.cc Feb 10 '14 at 06:40