Hi, the problem i'm having is with the following code. When the user clicks the submit button with any errors it displays the section I've marked out. But it positions this at the very top of my form, pushing evverything down. Is there a way to get this error information into a different place? Maybe output it to a div or something?

Thanks for any help

The code:

<?php
//FIll out the settings below before using this script
$your_email = "mr.trilby@hotmail.co.uk";
$website = "Rizo Contact v1.9";
//BOTS TO BLOCK
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer|T8Abot|Syntryx|WinHttp|WebBandit|nicebot)/i";
//Check if known bot is visiting
if (preg_match($bots, $_SERVER["HTTP_USER_AGENT"])) {
 exit ("Sorry bots are not allowed here!");
}

//Known Exploits

$exploits = "/(content-type|bcc:|cc:|from:|reply-to:|javascript|onclick|onload)/i";

//Spam words
$spam_words = "/(viagra|poker|blackjack|porn|sex)/i";

// BAD WORDS
$words = "/(
bitch|dick|pussy|pussies|ass|fuck|cum|cumshot|cum shot|
gangbang|gang bang|god dammit|goddammit|viagra|anus|analsex
)/i";
//BAD WORD/SPAM WORD/EXPLOIT BLOCKER
function wordBlock($word) {
  //Make variables global
 global $words;
 global $spam_words;

 if (preg_match($words, $word)) {
  $word = preg_replace($words, "#####", $word);
 }

if(preg_match($spam_words,$word)){
  $word = preg_replace($spam_words,"$$$$",$word);
}
 return $word;
}

function ex_clean($clean){
   global $exploits;
   if(preg_match($exploits,$clean)){
$clean = preg_replace($exploits,"",$clean);

}
return $clean;
}
//CLean data function
function dataClean($data) {
 $data = addslashes(trim(rawurldecode(strip_tags($data))));
 $data = filter_var ($data,FILTER_SANITIZE_SPECIAL_CHARS);
 return $data;
}
//CREATE MAIN VARIABLES
$name = (isset ($_POST['name'])) ? dataClean(ex_clean($_POST['name'])) : FALSE;
$email = (isset ($_POST['email'])) ? dataClean(ex_clean(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL))) : FALSE;
$subject = (isset ($_POST['subject'])) ? dataClean(ex_clean($_POST['subject'])) : FALSE;
$comment = (isset ($_POST['message'])) ? wordBlock(dataClean($_POST['message'])) : FALSE;
$submit = (isset ($_POST['send'])) ? TRUE : FALSE;
$email_check = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i";
$spam = (isset($_POST['spam'])) ? dataClean($_POST['spam']) : FALSE;
$ip = $_SERVER["REMOTE_ADDR"];
$errors = array();


///////////////////////////////////THIS PART/////////////////////////////////


//Check if send button was clicked
if ($submit) {
  if(!$spam)
  {
    $errors[] = "Please enter the code seen inside the image this is to prevent automated submissions!!!";
  }
  if($spam)
  {
    if($spam !== $_SESSION['captcha'])
    {
      $errors[] = "The spam code you entered is incorrect!!!";
    }
  }
 if (!$name) {
  $errors[] = "Please enter a name!!!";
 }
 if ($name) {
   if(!ctype_alpha($name)){
                    $errors[] = "Name must contain only letters A-Z!!!";
                  }
 }

 if (!$email) {
  $errors[] = "Please enter an email address!";
 }
 if ($email) {
  if (!preg_match($email_check, $email)) {
   $errors[] = "The E-mail you entered is invalid!";
  }
 }
 if (!$subject) {
  $errors[] = "Please enter a subject!";
 }

 if (!$comment) {
  $errors[] = "Please don't leave the message field blank!";
 }
 
 //If bot trap is tripped exit the script
 if(isset($_POST['Email_address']) ? $_POST['Email_address'] : FALSE ){
   exit();
 }
//Check if any errors are present
 if (count($errors) > 0) {
  foreach ($errors AS $error) {
   print "&bull; $error <br />";
  }
 }
 else {


//////////////////////////////////////////////////////////////////////////////





//MESSAGE TO SEND TO ADMIN
//Create main headers
  $headers = "From: " . $website . " <$your_email> \n";
  $headers .= "Reply-to:" . $email . " \n";
  $headers .= "MIME-Version: 1.0\n";
  $headers .= "Content-Transfer-Encoding: 8bit\n";
  $headers .= "Content-Type: text/html; charset=UTF-8\n";
  $message = "";
  $message .= "<h1>New E-Mail From " . $website . "</h1><br /><br />";
  $message .= "<b>Senders IP:</b>" . $ip . "<br />";
  $message .= "<b>Senders Name:</b>" . $name . "<br />";
  $message .= "<b>Senders E-mail:</b>" . $email . "<br />";
  $message .= "<b>Senders Subject:</b>" . $subject . "<br />";
  $message .= "<b>Senders Message:</b>" . $comment . "<br />";
//E-mails subject
  $mail_subject = "New E-mail From " . $website . "";
/*
CHECK TO BE SURE FIRST E-MAIL TO ADMIN IS A SUCCESS AND SEND EMAIL TO ADMIN
OTHERWISE DON'T SEND AUTO RESPONCE
*/
  if (mail($your_email, $mail_subject, $message, $headers)) {
//UNSET ALL VARIABLES
   unset ($name, $email, $subject, $reason, $comment, $_REQUEST);
//JAVASCRIPT SUCCESS MESSAGE
   echo "
   <script type='text/javascript' language='JavaScript'>
   alert('Your message has been sent');
   </script>
   ";
//SUCCESS MESSAGE TO SHOW IF JAVASCRIPT IS DISABLED
   echo "<noscript><p>THANK YOU YOUR MESSAGE HAS BEEN SENT</p></noscript>";
/*
-----------------END MAIL BLOCK FOR SENDING TO ADMIN AND START AUTO RESPONCE SEND-----------------
*/
//AUTO RESPONCE MESSAGE
//Create main headers
   $headers = "From: " . $website . " <$your_email> \n";
   $headers .= "Reply-to:" . $your_email . " \n";
   $headers .= "MIME-Version: 1.0\n";
   $headers .= "Content-Transfer-Encoding: 8bit\n";
   $headers .= "Content-Type: text/html; charset=UTF-8\n";
   $message = "";
   $message .= "<h1>Thank You For Contacting Us </h1><br /><br />";
   $message .= "On behalf of <b>" . $website . "</b> we wanna thank you for contacting us and to let you know we will respond to your message as soon as possible thank you again.";
//E-mails subject
   $mail_subject = "Thank you for contacting " . $website . "";
//Send the email
   mail($email, $mail_subject, $message, $headers);
/*
-----------------END MAIL BLOCK FOR SENDING AUTO RESPONCE -----------------
*/
  }
  else {
   echo "
   <script type='text/javascript' language='JavaScript'>
   alert('Sorry could not send your message');
   </script>
   ";
   echo "<noscript><p style='color:red;'>SORRY COULD NOT SEND YOUR MESSAGE</p></noscript>";
  }
  UNSET($_SESSION['captcha']);
 }

}

?>

I want the error information to be shown in a div, is this possible?

Member Avatar for diafol

This is obviously somebody else's code, right? Why don't you ask the author?

This is obviously somebody else's code, right? Why don't you ask the author?

Yes of course it is, and because it just seemed like a general question.

This is more than possible when you are using classes. I always do this:

/*This class tests if the value supplied by the user is "name"*/
class catchErrors {
     //setting my variables
     var $storedvar;
     var $suppliedvar;

     //Hope you are familiar with Object Oriented PHP; this is my constructor 
     //I include the value supplied by the user as a parameter to redefine $suppliedvar

     function __construct($suppliedvar) {
           $this->suppliedvar =  $suppliedvar;
           //I define the stored value here
           $this->storedvar = "name"; 
        }
     
     //this is the function that actualy does the execution
     function test {
            if($this->suppliedvar != $this->storedvar) {
                  /*This class: Exception is a pre-defined class that came with my PHP. Im using php 5 by the way. You can also define your own Exception class if you like, but I've always had success with this one ;-)*/
                  throw new Exception('You have not supplied the name');
               }
        }
}

$suppliedvar = $_POST['name'];
$testing = new catchErrors($suppliedvar);
try {
      $testing->test($suppliedvar);
}

catch (Exception $e) {
    //This is where you can place your <div>
    echo "<div id="mydiv">";
    echo $e->getMessage();
    echo "</div>";
}

I could not run this code, because I do not have access to my machine right now. Forgive me if you find some bugs. Just correct some synthax if you do. Hope it runs

Thanks alot for that, appreciate the effort, worked perfectly :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.