I have a simple php mail server hookup in my nodeJS site to handle contact messages. For some reason (I am testing it from my local), it doesn't route the message to the assigned email address. I mention running it from local because I have heard that it may not work unless its from my live site running on Heroku. However,I literally cut and pasted the code from another app I have in which it does work from local. Here's the code for the contact_me.php:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'myapp#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To:$email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
I have gotten a little confused somewhere, but it seems that the submit button I created in my html (id='success') should be connected to the $to object somehow so that when the button is clicked, the message is sent to the email address associated with $to. Here's my html. Am I missing something here?
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<h2 class="featurette-heading" style="text-align: center;">Message</h2>
<h3 class="text-muted">Want to book an appointment? Send me a message and I'll contact you. Make sure you include your name, phone number, email, and when you'd like to book. It's that easy!</h3>
<form name="sentMessage" id="contactForm" novalidate>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
I have modified this message since the first answer. The first answer seemed to work but I am still getting an error that prevents the request from going through. This is the error message:
You're using PHP, not node.js!
You're missing a very basic thing: you have not given your form fields names, so when you check for them, there's nothing there! You're checking for name, email, phone, message, but your fields have no name attributes at all. You need to name them in your HTML - and no, id values will not work:
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<h2 class="featurette-heading" style="text-align: center;">Message</h2>
<h3 class="text-muted">Want to book an appointment? Send me a message and I'll contact you. Make sure you include your name, phone number, email, and when you'd like to book. It's that easy!</h3>
<form name="sentMessage" id="contactForm" novalidate>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<input type="text" class="form-control" placeholder="Name" id="name" name="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" name="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label></label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" name="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
Related
i have a static one page site, with two forms, that i want to extract the information of the fields of that forms then send it to my email
this is the code of the two forms:
my exact question is how can i creat a php page that can help me to handle the two forms and receive the info in my email
<!-- Reservation Section -->
<section id="reservation">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Reserve Your Tour</h2>
<h3 class="section-subheading ">Explore in Our <a class="page-scroll" href="#portfolio">Tours Secetion</a>, Make Your Mind, Make Your Reservation.</h3>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<form name="sentMessage" id="contactForm" novalidate>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name2" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email2" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone2" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Country *" id="country" required data-validation-required-message="Please enter your country .">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Number Of People *" id="number" required data-validation-required-message="Please enter The number of people your comming with.">
<p class="help-block text-danger"></p>
</div>
</div>
<!-- second half !!-->
<div class="col-md-6">
<div class="form-group">
<input type="tel" class="form-control" placeholder="Date of arrival *(MM/DD/YYY)" id="dateofarrival" required data-validation-required-message="Please enter your Arrival Date.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Date of departure * *(MM/DD/YYY)" id="dateofdeparture" required data-validation-required-message="Please enter your Date of departure *.">
<p class="help-block text-danger"></p>
</div>
<div class="checkbox checkbox-inline">
<label><input type="checkbox" value="ToursFatima4Days" required data-validation-required-message="Please chose one tour at least">-Tours Fatima 4 Days</label>
<label><input type="checkbox" value="ToursAicha5Days">-Tours Aicha 5 Days </label>
<label><input type="checkbox" value="ToursAssmae9Days">-Tours Assmae 9 Days</label>
<label><input type="checkbox" value="ToursZaina5Days">-Tours Zaina 5 Days</label>
<label><input type="checkbox" value="AdventuresSafaris">-Adventures Safaris</label>
</div>
<br><br>
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" class="btn btn-xl">Submit Reservation</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section> <!-- end Reservation seccession -->
<!-- Contact Section -->
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Contact Us</h2>
<h3 class="section-subheading text-muted">Let Us Know About Anything Going on Your Mind.</h3>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<form name="sentMessage" id="contactForm" method="post" action="mail/contact_me.php" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> novalidate>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" class="btn btn-xl">Send Message</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
You're looking for PHP's mail function. There's also mail scripts that you can use.
Check these out:
http://www.inmotionhosting.com/support/edu/website-design/using-php-and-mysql/how-to-create-a-custom-php-contact-form
http://www.inmotionhosting.com/support/email/send-email-from-a-page/using-phpmailer-to-send-mail-through-php
I am fairly new to bootstrap and this is the first I have used reCaptcha. I also don't have a lot of experience with PHP. I have designed numerous web sites and am very familiar with html(5) and css(3).
I can easily add the reCaptcha widget to my form and it displays correctly. However, I have no clue how to update my PHP file for the server side processing. Below is the code for my bootstrap contact form section and the code for my PHP file.
HTML (contact section):
<!-- Contact Section -->
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Contact Me</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div class="g-recaptcha" data-sitekey="mySitekey"></div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-success btn-lg">Send</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
PHP (where do I put the required server-side php code here?):
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'myEmail'; // This is where the form will send a message to.
$email_subject = "Portfolio Website Message: $name";
$email_body = "You have received a new message from your portfolio website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#mydomain.com\n"; // This is the email address the generated message will be from.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Thanks for your help!
I am fairly new to bootstrap. I have a contact form that is working properly. However, I would like to add a captcha to the form to help eliminate unwanted SPAM mail. How can I add a captcha to my existing form or any other "are you human" option to the form (i.e. would a simple checkbox work) and make it responsive as the current form is? Below is the code for my form:
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Contact Me</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-success btn-lg">Send</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
Google ReCaptcha is a great tool for this and Google is also preparing a Captcha where no user input would be needed.
To implement a ReCaptcha, you will need to do following:
Create Google ReCaptcha API key
Paste the code into your HTML. Basically a div and some JS.
Validate the response when the form is submitted.
You can follow the step-by-step tutorial here: https://bootstrapious.com/p/bootstrap-recaptcha.
This is the php file I have made to store the responses from the Contact page of my Html file which is also written at the end.
Even after submitting the data in the contact field I am not getting anything.
Please try to help me out with the error.
PHP Code
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'abcd#gmail.com'; // This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the
details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
HTML CODE
<form name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-success btn-lg">Send</button>
</div>
</div>
</form>
You are missing the method and name attributes.
You are not specifying the Form method . If you want to use post method, you should specify
method="post".
If you are not specifying the form method, the default method will be GET
Also, you should add the attribute name to each and every input fields, then only you can access those in post.
Your updated HTML code will be:
<form name="sentMessage" id="contactForm" novalidate method="post">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" name="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" name="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" name="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" name="submit" class="btn btn-success btn-lg">Send</button>
</div>
</div>
</form>
Look the method attribute in form and name attribute in input fields.
Hope this helps
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Contact Me</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<!-- To configure the contact form email address, go to mail/contact_me.php and update the email address in the PHP file on line 19. -->
<!-- The form should work on most web servers, but if the form is not working you may need to configure your web server differently. -->
<form name="sentMessage" id="contactForm" onsubmit="return validate()" action="contactme.php">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email Address" id="emailz" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<input type="submit" value="Submit" name="Submit" class="btn btn-success btn-lg"></input>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<-----PHP----->
<?php
if(empty($_POST['name']) ||
empty($_POST['emailz']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['emailz'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!!";
return false;
}
$name = $_POST['name'];
$email = $_POST['emailz'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from
$headers .= "Reply-To: $emailz";
$subject = 'New Message - Kieronb- $name';
$body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $emailz\n\nPhone: $phone\n\nMessage:\n$message";
mail("k#gmail.com",$subject,$body,$headers);
return true;
?>
SO, the above is the HTML and the PHP, now the majority of this code has been frankensteined from a bootstrap project, but i thought i would just have a play with the contact form too, but it is going all the way through to the missing arguments catch at the beginning of the PHP.
I cant see why the data is coming through empty- have i missed something painfully obvious?
I am running on a web server, not locally.
Thanks
Edit; thanks everyone, i forgot my method, and names on the form!
Your form fields do not have the name attribute. Without them their values are not submitted with the form.
<input type="email" class="form-control" placeholder="Email Address" id="emailz" required data-validation-required-message="Please enter your email address.">
should be
<input type="email" name="emailz" class="form-control" placeholder="Email Address" id="emailz" required data-validation-required-message="Please enter your email address.">
And your <form> needs to have the method specified as POST or else it defaults to GET:
<form method="post" name="sentMessage" id="contactForm" onsubmit="return validate()" action="contactme.php">