943,171 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 26
  • PHP RSS
Aug 31st, 2010
0

Errors sent to DIV?

Expand Post »
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 Syntax (Toggle Plain Text)
  1. <?php
  2. //FIll out the settings below before using this script
  3. $your_email = "mr.trilby@hotmail.co.uk";
  4. $website = "Rizo Contact v1.9";
  5. //BOTS TO BLOCK
  6. $bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer|T8Abot|Syntryx|WinHttp|WebBandit|nicebot)/i";
  7. //Check if known bot is visiting
  8. if (preg_match($bots, $_SERVER["HTTP_USER_AGENT"])) {
  9. exit ("Sorry bots are not allowed here!");
  10. }
  11.  
  12. //Known Exploits
  13.  
  14. $exploits = "/(content-type|bcc:|cc:|from:|reply-to:|javascript|onclick|onload)/i";
  15.  
  16. //Spam words
  17. $spam_words = "/(viagra|poker|blackjack|porn|sex)/i";
  18.  
  19. // BAD WORDS
  20. $words = "/(
  21. bitch|dick|pussy|pussies|ass|****|cum|cumshot|cum shot|
  22. gangbang|gang bang|god dammit|goddammit|viagra|anus|analsex
  23. )/i";
  24. //BAD WORD/SPAM WORD/EXPLOIT BLOCKER
  25. function wordBlock($word) {
  26. //Make variables global
  27. global $words;
  28. global $spam_words;
  29.  
  30. if (preg_match($words, $word)) {
  31. $word = preg_replace($words, "#####", $word);
  32. }
  33.  
  34. if(preg_match($spam_words,$word)){
  35. $word = preg_replace($spam_words,"$$$$",$word);
  36. }
  37. return $word;
  38. }
  39.  
  40. function ex_clean($clean){
  41. global $exploits;
  42. if(preg_match($exploits,$clean)){
  43. $clean = preg_replace($exploits,"",$clean);
  44.  
  45. }
  46. return $clean;
  47. }
  48. //CLean data function
  49. function dataClean($data) {
  50. $data = addslashes(trim(rawurldecode(strip_tags($data))));
  51. $data = filter_var ($data,FILTER_SANITIZE_SPECIAL_CHARS);
  52. return $data;
  53. }
  54. //CREATE MAIN VARIABLES
  55. $name = (isset ($_POST['name'])) ? dataClean(ex_clean($_POST['name'])) : FALSE;
  56. $email = (isset ($_POST['email'])) ? dataClean(ex_clean(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL))) : FALSE;
  57. $subject = (isset ($_POST['subject'])) ? dataClean(ex_clean($_POST['subject'])) : FALSE;
  58. $comment = (isset ($_POST['message'])) ? wordBlock(dataClean($_POST['message'])) : FALSE;
  59. $submit = (isset ($_POST['send'])) ? TRUE : FALSE;
  60. $email_check = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i";
  61. $spam = (isset($_POST['spam'])) ? dataClean($_POST['spam']) : FALSE;
  62. $ip = $_SERVER["REMOTE_ADDR"];
  63. $errors = array();
  64.  
  65.  
  66. ///////////////////////////////////THIS PART/////////////////////////////////
  67.  
  68.  
  69. //Check if send button was clicked
  70. if ($submit) {
  71. if(!$spam)
  72. {
  73. $errors[] = "Please enter the code seen inside the image this is to prevent automated submissions!!!";
  74. }
  75. if($spam)
  76. {
  77. if($spam !== $_SESSION['captcha'])
  78. {
  79. $errors[] = "The spam code you entered is incorrect!!!";
  80. }
  81. }
  82. if (!$name) {
  83. $errors[] = "Please enter a name!!!";
  84. }
  85. if ($name) {
  86. if(!ctype_alpha($name)){
  87. $errors[] = "Name must contain only letters A-Z!!!";
  88. }
  89. }
  90.  
  91. if (!$email) {
  92. $errors[] = "Please enter an email address!";
  93. }
  94. if ($email) {
  95. if (!preg_match($email_check, $email)) {
  96. $errors[] = "The E-mail you entered is invalid!";
  97. }
  98. }
  99. if (!$subject) {
  100. $errors[] = "Please enter a subject!";
  101. }
  102.  
  103. if (!$comment) {
  104. $errors[] = "Please don't leave the message field blank!";
  105. }
  106.  
  107. //If bot trap is tripped exit the script
  108. if(isset($_POST['Email_address']) ? $_POST['Email_address'] : FALSE ){
  109. exit();
  110. }
  111. //Check if any errors are present
  112. if (count($errors) > 0) {
  113. foreach ($errors AS $error) {
  114. print "&bull; $error <br />";
  115. }
  116. }
  117. else {
  118.  
  119.  
  120. //////////////////////////////////////////////////////////////////////////////
  121.  
  122.  
  123.  
  124.  
  125.  
  126. //MESSAGE TO SEND TO ADMIN
  127. //Create main headers
  128. $headers = "From: " . $website . " <$your_email> \n";
  129. $headers .= "Reply-to:" . $email . " \n";
  130. $headers .= "MIME-Version: 1.0\n";
  131. $headers .= "Content-Transfer-Encoding: 8bit\n";
  132. $headers .= "Content-Type: text/html; charset=UTF-8\n";
  133. $message = "";
  134. $message .= "<h1>New E-Mail From " . $website . "</h1><br /><br />";
  135. $message .= "<b>Senders IP:</b>" . $ip . "<br />";
  136. $message .= "<b>Senders Name:</b>" . $name . "<br />";
  137. $message .= "<b>Senders E-mail:</b>" . $email . "<br />";
  138. $message .= "<b>Senders Subject:</b>" . $subject . "<br />";
  139. $message .= "<b>Senders Message:</b>" . $comment . "<br />";
  140. //E-mails subject
  141. $mail_subject = "New E-mail From " . $website . "";
  142. /*
  143. CHECK TO BE SURE FIRST E-MAIL TO ADMIN IS A SUCCESS AND SEND EMAIL TO ADMIN
  144. OTHERWISE DON'T SEND AUTO RESPONCE
  145. */
  146. if (mail($your_email, $mail_subject, $message, $headers)) {
  147. //UNSET ALL VARIABLES
  148. unset ($name, $email, $subject, $reason, $comment, $_REQUEST);
  149. //JAVASCRIPT SUCCESS MESSAGE
  150. echo "
  151. <script type='text/javascript' language='JavaScript'>
  152. alert('Your message has been sent');
  153. </script>
  154. ";
  155. //SUCCESS MESSAGE TO SHOW IF JAVASCRIPT IS DISABLED
  156. echo "<noscript><p>THANK YOU YOUR MESSAGE HAS BEEN SENT</p></noscript>";
  157. /*
  158. -----------------END MAIL BLOCK FOR SENDING TO ADMIN AND START AUTO RESPONCE SEND-----------------
  159. */
  160. //AUTO RESPONCE MESSAGE
  161. //Create main headers
  162. $headers = "From: " . $website . " <$your_email> \n";
  163. $headers .= "Reply-to:" . $your_email . " \n";
  164. $headers .= "MIME-Version: 1.0\n";
  165. $headers .= "Content-Transfer-Encoding: 8bit\n";
  166. $headers .= "Content-Type: text/html; charset=UTF-8\n";
  167. $message = "";
  168. $message .= "<h1>Thank You For Contacting Us </h1><br /><br />";
  169. $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.";
  170. //E-mails subject
  171. $mail_subject = "Thank you for contacting " . $website . "";
  172. //Send the email
  173. mail($email, $mail_subject, $message, $headers);
  174. /*
  175. -----------------END MAIL BLOCK FOR SENDING AUTO RESPONCE -----------------
  176. */
  177. }
  178. else {
  179. echo "
  180. <script type='text/javascript' language='JavaScript'>
  181. alert('Sorry could not send your message');
  182. </script>
  183. ";
  184. echo "<noscript><p style='color:red;'>SORRY COULD NOT SEND YOUR MESSAGE</p></noscript>";
  185. }
  186. UNSET($_SESSION['captcha']);
  187. }
  188.  
  189. }
  190.  
  191. ?>

I want the error information to be shown in a div, is this possible?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ello is offline Offline
66 posts
since Aug 2010
Aug 31st, 2010
0
Re: Errors sent to DIV?
This is obviously somebody else's code, right? Why don't you ask the author?
Sponsor
Featured Poster
Reputation Points: 1041
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,620 posts
since Oct 2006
Aug 31st, 2010
0
Re: Errors sent to DIV?
Click to Expand / Collapse  Quote originally posted by ardav ...
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ello is offline Offline
66 posts
since Aug 2010
Sep 1st, 2010
0
Re: Errors sent to DIV?
This is more than possible when you are using classes. I always do this:

PHP Syntax (Toggle Plain Text)
  1. /*This class tests if the value supplied by the user is "name"*/
  2. class catchErrors {
  3. //setting my variables
  4. var $storedvar;
  5. var $suppliedvar;
  6.  
  7. //Hope you are familiar with Object Oriented PHP; this is my constructor
  8. //I include the value supplied by the user as a parameter to redefine $suppliedvar
  9.  
  10. function __construct($suppliedvar) {
  11. $this->suppliedvar = $suppliedvar;
  12. //I define the stored value here
  13. $this->storedvar = "name";
  14. }
  15.  
  16. //this is the function that actualy does the execution
  17. function test {
  18. if($this->suppliedvar != $this->storedvar) {
  19. /*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 ;-)*/
  20. throw new Exception('You have not supplied the name');
  21. }
  22. }
  23. }
  24.  
  25. $suppliedvar = $_POST['name'];
  26. $testing = new catchErrors($suppliedvar);
  27. try {
  28. $testing->test($suppliedvar);
  29. }
  30.  
  31. catch (Exception $e) {
  32. //This is where you can place your <div>
  33. echo "<div id="mydiv">";
  34. echo $e->getMessage();
  35. echo "</div>";
  36. }

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
Last edited by sithembisophp; Sep 1st, 2010 at 3:19 am. Reason: I wanted to add more information
Reputation Points: 10
Solved Threads: 3
Newbie Poster
sithembisophp is offline Offline
14 posts
since Aug 2010
Sep 1st, 2010
0
Re: Errors sent to DIV?
Thanks alot for that, appreciate the effort, worked perfectly
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
ello is offline Offline
66 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need hdlp with this script! I have tried everything but what will work.
Next Thread in PHP Forum Timeline: Video





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC