jazza96 Posted February 8, 2015 Share Posted February 8, 2015 Hi I am trying to build a contact form and my form isnt sending. Here is my code. (its in a modal window)(I have my email address in the $to variable in the code) <?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: Jarrod'; $to = 'email'; $subject = "Landing page"; $body = "From: $name\n Email: $email\n Message:\n $message"; ?> <?php if ($_POST['submit']){ if(mail ($to, $subject, $body, $from)) { echo '<p>Your message has been sent!</p>'; }else{ echo '<p>Something went wrong, go back!</p>'; } } ?> <div class="modal fade" id="contact" role="dialog"> <div class=" modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h2>Contact us</h2> </div> <div class="modal-body"> <form method="post" action="index1.php"> <div class="form-horizontal"> <label for="Name">Your Name</label> <input type="text" class="form-control" id="name" placeholder="Name"> <label for="Email">Your Email</label> <input type="Email" class="form-control" id="email" placeholder="Email"> <label for="Message">Message</label> <textarea class="form-control" id="message" placeholder="Message"></textarea> <input type="submit" value="Send">Send</button> </form> <div class="modal-footer"> <a class="btn btn-default" data-dismiss = "modal">Close</a> </div> </div> </div> </div> Quote Link to comment Share on other sites More sharing options...
Chezshire Posted February 8, 2015 Share Posted February 8, 2015 I don't see an action set on your form, that might be something to look at. An action is the url/address that the form is sending too. example: <form action="action_page.php"> First name:<br> <input type="text" name="firstname" value="Mickey"> <br> Last name:<br> <input type="text" name="lastname" value="Mouse"> <br><br> <input type="submit" value="Submit"> </form> Quote Link to comment Share on other sites More sharing options...
Barand Posted February 8, 2015 Share Posted February 8, 2015 (edited) You are setting $to to the string value 'email' and not to the posted email value in $email. $to = 'email'; Edited February 8, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 9, 2015 Author Share Posted February 9, 2015 You are setting $to to the string value 'email' and not to the posted email value in $email. Yes, I did that on purpose because I didn't want my email address public. Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 9, 2015 Author Share Posted February 9, 2015 I don't see an action set on your form, that might be something to look at. An action is the url/address that the form is sending too. example: <form action="action_page.php"> First name:<br> <input type="text" name="firstname" value="Mickey"> <br> Last name:<br> <input type="text" name="lastname" value="Mouse"> <br><br> <input type="submit" value="Submit"> </form> I have added an action to the form. <div class="modal-body"> <form method="post" action="index.php"> <div class="form-horizontal"> <label for="Name">Your Name</label> <input type="text" class="form-control" id="Name" placeholder="Name"> <label for="Email">Your Email</label> <input type="Email" class="form-control" id="Email" placeholder="Email"> <label for="Message">Message</label> <textarea class="form-control" id="Message" placeholder="Message"></textarea> <button type="button" class="btn btn-primary">Send</button> </form> </div> </div> The php code is on the index.php file. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 9, 2015 Share Posted February 9, 2015 Add names to your form fields not id (Otherwise they will be blank) also to the submit button <input type="text" class="form-control" name="name" placeholder="Name"> <label for="Email">Your Email</label> <input type="Email" class="form-control" name="email" placeholder="Email"> <label for="Message">Message</label> <textarea class="form-control" name="message" placeholder="Message"></textarea> <input type="submit" name="submit" value="Send">Send</button> Quote Link to comment Share on other sites More sharing options...
maxxd Posted February 9, 2015 Share Posted February 9, 2015 Do you have error reporting turned on? It looks like you're attempting to assign values in $_POST to variables before you're actually checking to see if the form has been submitted. That could be throwing you off. Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 9, 2015 Author Share Posted February 9, 2015 Add names to your form fields not id (Otherwise they will be blank) also to the submit button <input type="text" class="form-control" name="name" placeholder="Name"> <label for="Email">Your Email</label> <input type="Email" class="form-control" name="email" placeholder="Email"> <label for="Message">Message</label> <textarea class="form-control" name="message" placeholder="Message"></textarea> <input type="submit" name="submit" value="Send">Send</button> Tried it and it didn't work unfortunately Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 9, 2015 Author Share Posted February 9, 2015 Do you have error reporting turned on? It looks like you're attempting to assign values in $_POST to variables before you're actually checking to see if the form has been submitted. That could be throwing you off. I am getting 4 undefined errors. Notice: Undefined index: name in /Applications/MAMP/htdocs/Jumbotron/index.php on line 124 Notice: Undefined index: email in /Applications/MAMP/htdocs/Jumbotron/index.php on line 125 Notice: Undefined index: message in /Applications/MAMP/htdocs/Jumbotron/index.php on line 126 Notice: Undefined index: submit in /Applications/MAMP/htdocs/Jumbotron/index.php on line 135 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 9, 2015 Share Posted February 9, 2015 I am getting 4 undefined errors. Do you get the error messages after the form submits? Note: to avoid seeing the errors before the form is submitted, move the initial variable declarations inside the construct that tests whether the form was submitted: <?php if ($_POST['submit']){ $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: Jarrod'; $to = 'email'; $subject = "Landing page"; $body = "From: $name\n Email: $email\n Message:\n $message"; if(mail ($to, $subject, $body, $from)) { echo '<p>Your message has been sent!</p>'; }else{ echo '<p>Something went wrong, go back!</p>'; } } ?> Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 9, 2015 Author Share Posted February 9, 2015 (edited) Do you get the error messages after the form submits? Note: to avoid seeing the errors before the form is submitted, move the initial variable declarations inside the construct that tests whether the form was submitted: <?php if ($_POST['submit']){ $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: Jarrod'; $to = 'email'; $subject = "Landing page"; $body = "From: $name\n Email: $email\n Message:\n $message"; if(mail ($to, $subject, $body, $from)) { echo '<p>Your message has been sent!</p>'; }else{ echo '<p>Something went wrong, go back!</p>'; } } ?> Do you get the error messages after the form submits? Note: to avoid seeing the errors before the form is submitted, move the initial variable declarations inside the construct that tests whether the form was submitted: <?php if ($_POST['submit']){ $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'From: Jarrod'; $to = 'email'; $subject = "Landing page"; $body = "From: $name\n Email: $email\n Message:\n $message"; if(mail ($to, $subject, $body, $from)) { echo '<p>Your message has been sent!</p>'; }else{ echo '<p>Something went wrong, go back!</p>'; } } ?> The errors are appearing when I refresh the page. I put in that code and I am no longer getting this error: Notice: Undefined index: submit in /Applications/MAMP/htdocs/Jumbotron/index.php on line 135 but I am still getting the other error's Notice: Undefined index: name in /Applications/MAMP/htdocs/Jumbotron/index.php on line 124Notice: Undefined index: email in /Applications/MAMP/htdocs/Jumbotron/index.php on line 125Notice: Undefined index: message in /Applications/MAMP/htdocs/Jumbotron/index.php on line 126 Edited February 9, 2015 by jazza96 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 9, 2015 Share Posted February 9, 2015 Ah, it looks like you need to name the submit button also. Try changing this <input type="submit" value="Send">Send</button> To this <input type="submit" name="submit" value="Send"> The name attribute tells PHP to create the POST variable used here: if($_POST['submit']) { Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 9, 2015 Share Posted February 9, 2015 Side note: the id attribute in your original code is used to connect the form label with the corresponding field. <label for="Name">Your Name</label> <input type="text" class="form-control" id="name" name="name" placeholder="Name"> Of course, the <label> tag wasn't successfully connected since the for attribute doesn't match the id attribute. One was set to "Name" and the other is set to "name"...note the case difference. With that said, you could just surround the input field with the label tag: <label>Your Name <input type="text" class="form-control" name="name" placeholder="Name"></label> More information about the <label> tag can be found here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 9, 2015 Author Share Posted February 9, 2015 Ah, it looks like you need to name the submit button also. Try changing this <input type="submit" value="Send">Send</button> To this <input type="submit" name="submit" value="Send"> The name attribute tells PHP to create the POST variable used here: if($_POST['submit']) { It worked!! Thanks One other problem is that the message is not sending now to my nominated email address. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 9, 2015 Share Posted February 9, 2015 One other problem is that the message is not sending now to my nominated email address. did you set the $to back to your email after making all the changes? Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 9, 2015 Author Share Posted February 9, 2015 did you set the $to back to your email after making all the changes? Yes I did. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 9, 2015 Share Posted February 9, 2015 Have you sent email from this site successfully in the past? Might want to post the updated code you are using now Quote Link to comment Share on other sites More sharing options...
CroNiX Posted February 9, 2015 Share Posted February 9, 2015 $from should contain an actual email address on your server as well, or at least the correct domain. There are a lot of spam filters out there that won't accept just "From: Jarrod" where "From: email@this_site.com" would work, as long as "this_site.com" is the actual site name sending the email. Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 10, 2015 Author Share Posted February 10, 2015 Have you sent email from this site successfully in the past? Might want to post the updated code you are using now No, I have not. It is just on my localhost server (MAMP). <?php error_reporting(-1); ini_set('display_errors', 'On'); $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'email'; $to = 'email'; /* I have my different email in these fields. */ $subject = "Landing page"; $body = "From: $name\n email: $email\n message:\n $message"; ?> <?php if(mail ($to, $subject, $body, $from)) { echo '<p>Your message has been sent</p>'; }else{ echo '<p>Something went wrong, go back!</p>'; } ?> <div class="modal fade" id="contact" role="dialog"> <div class=" modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h2>Contact us</h2> <p>Do you have a question, suggestion or comment?</p> </div> <div class="modal-body"> <form method="post" action="index.php"> <div class="form-horizontal"> <label for="Name">Your Name</label> <input type="text" class="form-control" name="name" placeholder="Name"> <label for="Email">Your Email</label> <input type="Email" class="form-control" name="email" placeholder="Email"> <label for="Message">Message</label> <textarea class="form-control" name="message" placeholder="Message"></textarea> <input type="submit" name="submit" value="Send" action="index.php"></input> </form> </div> </div> <div class="modal-footer"> <a class="btn btn-default" data-dismiss = "modal">Close</a> </div> </div> </div> Quote Link to comment Share on other sites More sharing options...
jazza96 Posted February 10, 2015 Author Share Posted February 10, 2015 $from should contain an actual email address on your server as well, or at least the correct domain. There are a lot of spam filters out there that won't accept just "From: Jarrod" where "From: email@this_site.com" would work, as long as "this_site.com" is the actual site name sending the email. Yes, I put an email address in the $from variable and it still didn't work. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 10, 2015 Share Posted February 10, 2015 I would make sure it is configured to send mail Maybe This link can explain how Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.