Lucy Posted August 14, 2014 Share Posted August 14, 2014 (edited) I'm using the following PHP to send information from a contact form on my website to my email address: <?php $errors = '';$myemail = ''; if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])){ $errors .= "n Error: all fields are required";}$name = $_POST['name']; $company = $_POST['company'];$email_address = $_POST['email']; $message = $_POST['message']; if (!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $email_address)){ $errors .= "n Error: Invalid email address";}if( empty($errors)){ $to = $myemail; $email_subject = "Contact form submission: $name"; $email_body = "You have received a new message. ". " Here are the details:n Name: $name n Company: $company n Email: $email_address n Message: $message"; $headers = "From: $myemailn"; $headers .= "Reply-To: $email_address"; mail($to,$email_subject,$email_body,$headers); //redirect to the 'thank you' page} header('Location: thankyou.html'); ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head> <title>Contact form handler</title></head><body><!-- This page is displayed only if there is some error --><?phpecho nl2br($errors);?></body></html> It's from a website, free for use, can't remember where though. I have an additional field to the default ones in my form - 'company'. As you can see, I've tried to add in extra bits for it, but I still get an empty contact field in the email every time I test it. I should point out, I really don't know what I'm doing with PHP. Not sure where I'm going wrong with this, any ideas? Edited August 26, 2014 by Lucy Link to comment Share on other sites More sharing options...
niche Posted August 14, 2014 Share Posted August 14, 2014 (edited) It would be best for you to do these php form/email tutorials instead of trying to fix this script IMO. http://www.w3schools.com/php/php_forms.asp http://www.w3schools.com/php/php_mail.asp For example. you said you have a problem with an empty contact field, but I don't see one in your script. Edited August 14, 2014 by niche Link to comment Share on other sites More sharing options...
JamesB Posted August 14, 2014 Share Posted August 14, 2014 Can you post your HTML code for the form? Link to comment Share on other sites More sharing options...
Lucy Posted August 14, 2014 Author Share Posted August 14, 2014 <form name="contactform" method="POST" action="contact-form-handler.php" id="contact"> <div id="left"> <label for="name" class="lab">name:</label> <input type="text" name="name" class="fh" required> <label for "company" class="lab">company:</label> <input type="text" name="company" class="fh"> <label for "email" class="lab">email:</label> <input type="email" name="email" class="fh" required> </div> <div id="right"> <label for="message" class="lab">message:</label> <textarea name="message" rows="10" class="mes" required></textarea><br/> <input type="submit" value="send" id="send"> </div> </form> There you go I could always just do away with the company field, I suppose, if it won't work. Link to comment Share on other sites More sharing options...
davej Posted August 14, 2014 Share Posted August 14, 2014 Shouldn't this line be inside the curly brace above it? header('Location: thankyou.html'); You have missing equals signs in these lines... <label for "company" class="lab">company:</label>...<label for "email" class="lab">email:</label> Link to comment Share on other sites More sharing options...
Lucy Posted August 14, 2014 Author Share Posted August 14, 2014 Ah, of course... I honestly spent hours staring at all this trying to spot something like that Thanks so much, it works perfectly now Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now