Note that there are some explanatory texts on larger screens.

plurals
  1. POContact Form won't send email on localhost
    primarykey
    data
    text
    <p>I'm trying to create a contactform which is placed under a sticky button on the right hand side.</p> <p>It seems my form isn't submitting at all. I tried to put the code on seperate files using <code>action="contactus.php"</code> without any result. I also tried it on the same page, but still no luck. Can someone give me advise on this one please?</p> <p>PHP in the HEAD-section:</p> <pre><code>&lt;?php define("EMAIL", "my@email.com"); if(isset($_POST['submit'])) { include('validate.class.php'); //assign post data to variables $name = trim(mysql_real_escape_string($_POST['name'])); $email = trim(mysql_real_escape_string($_POST['email'])); $message = trim(mysql_real_escape_string($_POST['message'])); //start validating our form $v = new validate(); $v-&gt;validateStr($name, "name", 3, 75); $v-&gt;validateEmail($email, "email"); $v-&gt;validateStr($message, "message", 5, 1000); if(!$v-&gt;hasErrors()) { $header = "From: $email\n" . "Reply-To: $email\n"; $subject = "Contact Form Subject"; $email_to = EMAIL; $emailMessage = "Name: " . $name . "\n"; $emailMessage .= "Email: " . $email . "\n\n"; $emailMessage .= $message; //use php's mail function to send the email @mail($email_to, $subject, $emailMessage, $header ); //grab the current url, append ?sent=yes to it and then redirect to that url $url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; header('Location: '.$url."?sent=yes"); } else { //set the number of errors message $message_text = $v-&gt;errorNumMessage(); //store the errors list in a variable $errors = $v-&gt;displayErrors(); //get the individual error messages $nameErr = $v-&gt;getError("name"); $emailErr = $v-&gt;getError("email"); $messageErr = $v-&gt;getError("message"); }//end error check }// end isset ?&gt; </code></pre> <p>Contact form in HTML:</p> <pre><code>&lt;div class="slide-out-div"&gt; &lt;a class="handle"&gt;Content&lt;/a&gt; &lt;h3&gt;Leave your details and we will get back to you asap!&lt;/h3&gt; &lt;br&gt; &lt;form id="contact_form" method="post" action=""&gt; Name &lt;br /&gt;&lt;input type="text" name="name" required&gt; &lt;br /&gt;&lt;br /&gt; Email &lt;br /&gt;&lt;input type="email" name="email" required&gt; &lt;br /&gt;&lt;br /&gt; Message &lt;br /&gt;&lt;textarea name="message" class="textarea" cols="30" rows="5" required&gt;&lt;/textarea&gt; &lt;br /&gt;&lt;br /&gt; &lt;p&gt;&lt;input type="submit" name="submit" class="button" value="Submit" /&gt;&lt;/p&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>The validate.class.php file:</p> <pre><code>&lt;?php class validate { // --------------------------------------------------------------------------- // paramaters // --------------------------------------------------------------------------- /** * Array to hold the errors * * @access public * @var array */ public $errors = array(); // --------------------------------------------------------------------------- // validation methods // --------------------------------------------------------------------------- /** * Validates a string * * @access public * @param $postVal - the value of the $_POST request * @param $postName - the name of the form element being validated * @param $min - minimum string length * @param $max - maximum string length * @return void */ public function validateStr($postVal, $postName, $min = 5, $max = 500) { if(strlen($postVal) &lt; intval($min)) { $this-&gt;setError($postName, ucfirst($postName)." must be at least {$min} characters long."); } else if(strlen($postVal) &gt; intval($max)) { $this-&gt;setError($postName, ucfirst($postName)." must be less than {$max} characters long."); } }// end validateStr /** * Validates an email address * * @access public * @param $emailVal - the value of the $_POST request * @param $emailName - the name of the email form element being validated * @return void */ public function validateEmail($emailVal, $emailName) { if(strlen($emailVal) &lt;= 0) { $this-&gt;setError($emailName, "Please enter an Email Address"); } else if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $emailVal)) { $this-&gt;setError($emailName, "Please enter a Valid Email Address"); } }// end validateEmail // --------------------------------------------------------------------------- // error handling methods // --------------------------------------------------------------------------- /** * sets an error message for a form element * * @access private * @param string $element - name of the form element * @param string $message - error message to be displayed * @return void */ private function setError($element, $message) { $this-&gt;errors[$element] = $message; }// end logError /** * returns the error of a single form element * * @access public * @param string $elementName - name of the form element * @return string */ public function getError($elementName) { if($this-&gt;errors[$elementName]) { return $this-&gt;errors[$elementName]; } else { return false; } }// end getError /** * displays the errors as an html un-ordered list * * @access public * @return string: A html list of the forms errors */ public function displayErrors() { $errorsList = "&lt;ul class=\"errors\"&gt;\n"; foreach($this-&gt;errors as $value) { $errorsList .= "&lt;li&gt;". $value . "&lt;/li&gt;\n"; } $errorsList .= "&lt;/ul&gt;\n"; return $errorsList; }// end displayErrors /** * returns whether the form has errors * * @access public * @return boolean */ public function hasErrors() { if(count($this-&gt;errors) &gt; 0) { return true; } else { return false; } }// end hasErrors /** * returns a string stating how many errors there were * * @access public * @return void */ public function errorNumMessage() { if(count($this-&gt;errors) &gt; 1) { $message = "There were " . count($this-&gt;errors) . " errors sending your message!\n"; } else { $message = "There was an error sending your message!\n"; } return $message; }// end hasErrors }// end class ?&gt; </code></pre> <p>When I see what the POST-data is in FireBug I see the actual data which I filled in. Is it possible this isn't working because I'm on LocalHost?</p> <p>Kind regards</p> <p>Bjorn</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload