2d2f Posted July 27, 2014 Share Posted July 27, 2014 <form action="form-to-email.php" method="post" enctype="text/plain" class="form"> <p class="name"> <i class="icon-user"></i> <input type="text" name="name" id="name" /> <label for="name">Name</label> </p> <p class="email"> <i class="icon-envelope"></i><span id="sprytextfield1"> <input type="text" name="email" id="email" /> </span> <label for="email">E-mail</label> </p> <p class="phone"><i class="icon-phone-sign"></i> <input type="text" name="phone" id="phone" /> <label for="web">Phone</label> </p> <p class="text"><span id="sprytextarea1"> <textarea name="text"></textarea> <span class="textareaRequiredMsg">A value is required.</span></span></p> <p class="submit"> <input type="submit" name="submit" id="submit" value="Send message"> </p> </form> The form is located in index.html and separate page is form-to-email.php. Do I need to link them <link href=>, <link rel=> or the files just need to be in the same folder? <?php if(!isset($_POST['submit'])) { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; } $name = $_POST['name']; $visitor_email = $_POST['email']; $message = $_POST['message']; //Validate first if(empty($name)||empty($visitor_email)) { echo "Name and email are mandatory!"; exit; } if(IsInjected($visitor_email)) { echo "Bad email value!"; exit; } $email_from = '$visitor_email \r\n"'; $email_subject = "New Form submission"; $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $message". $to = "2d2f@gmail.com";//<== update the email address $headers = "From: $email_from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; //Send the email! mail($to,$email_subject,$email_body,$headers); //done. redirect to thank-you page. header('Location: thank-you.html'); Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/ Share on other sites More sharing options...
trq Posted July 27, 2014 Share Posted July 27, 2014 No you don't need to "link" them, the form just needs to submit to your php script (by setting it as the action). Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486276 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 No you don't need to "link" them, the form just needs to submit to your php script (by setting it as the action). Should it look like this? <p class="submit"> <input type="submit" formaction="form-to-email.php" name="submit" id="submit" value="Send message"> </p> If I click submit i get this: Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486278 Share on other sites More sharing options...
trq Posted July 28, 2014 Share Posted July 28, 2014 Should it look like this? <p class="submit"> <input type="submit" formaction="form-to-email.php" name="submit" id="submit" value="Send message"> </p> No, the code you're have in your form is fine. Judging from your error, you don't have a server setup. PHP requires a http server configured to execute PHP. You then need to access your site via urls, not file paths. eg; http://localhost/index.html, not file:///C:/ etc Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486280 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 Thanks. I wanted to try it before it goes "live", because I'm not sure that it's working, if you say the code is ok I ll try and let you know . Thanks Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486281 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 Not working returns "echo "error; you need to submit the form!";" Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486283 Share on other sites More sharing options...
trq Posted July 28, 2014 Share Posted July 28, 2014 Thanks. I wanted to try it before it goes "live", because I'm not sure that it's working, if you say the code is ok I ll try and let you know . Thanks You "try before it goes live" by installing a local http server to use when developing. You won't get any php working without one. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486287 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 You "try before it goes live" by installing a local http server to use when developing. You won't get any php working without one. Ok. I tried it by uploading files to the hosting server, without success. When I delete the validation part the mail goes trough, but there is no data. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486297 Share on other sites More sharing options...
cyberRobot Posted July 28, 2014 Share Posted July 28, 2014 I'm not sure if this is causing the issue, but you're missing a semi-colon. This $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $message". Should be: $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $message"; Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486303 Share on other sites More sharing options...
cyberRobot Posted July 28, 2014 Share Posted July 28, 2014 It looks like the problem is caused by the "enctype" attribute in the <form> tag. Is there a reason you are using the following: enctype="text/plain" Your code seems to work for me once the attribute is removed. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486304 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 Fatal error: Call to undefined function IsInjected() in /home/public_html/www.2d2f.com/form-to-email.php on line 11 Now I get this Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486324 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 <form action="form-to-email.php" method="post" class="form"> <p class="name"> <i class="icon-user"></i> <input type="text" name="name" id="name" /> <label for="name">Name</label> </p> <p class="email"> <i class="icon-envelope"></i><span id="sprytextfield1"> <input type="text" name="email" id="email" /> </span> <label for="email">E-mail</label> </p> <p class="phone"><i class="icon-phone-sign"></i> <input type="text" name="phone" id="phone" /> <label for="web">Phone</label> </p> <p class="text"><span id="sprytextarea1"> <textarea name="text"></textarea> <span class="textareaRequiredMsg">A value is required.</span></span></p> <p class="submit"> <input type="submit" name="submit" id="submit" value="Send message"> </p> </form> <?php if(!isset($_POST['submit'])) { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; } $name = $_POST['name']; $visitor_email = $_POST['email']; $message = $_POST['message']; if(IsInjected($visitor_email)) { echo "Bad email value!"; exit; } $email_from = '$visitor_email \r\n"'; $email_subject = "New Form submission"; $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $message"; $to = "2d2f@gmail.com";//<== update the email address $headers = "From: $email_from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; //Send the email! mail($to,$email_subject,$email_body,$headers); //done. redirect to thank-you page. header('Location: thank-you.html'); I think that I have made the suggested changes. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486325 Share on other sites More sharing options...
Jacques1 Posted July 28, 2014 Share Posted July 28, 2014 (edited) I wonder where on earth you got the text/plain from in the first place. Let me guess: w3schools? This is an incredibly exotic encoding which is explicitly marked as “for humans only”, so you're unlikely to find any language which parses this. In fact, I've never seen it being used in real life (outside of w3schools). In any case: You need a better learning resource. Edited July 28, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486326 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 I removed this code and it sends an emai, but picks up only the name from the form if(IsInjected($visitor_email)) { echo "Bad email value!"; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486328 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 I wonder where on earth you got the text/plain from in the first place. Let me guess: w3schools? This is an incredibly exotic encoding which is explicitly marked as “for humans only”, so you're unlikely to find any language which parses this. In fact, I've never seen it being used in real life (outside of w3schools). In any case: You need a better learning resource. Sorry, this is the first time that I'm dealing with php. I found the code online. Maybe here http://code.tutsplus.com/tutorials/design-a-prettier-web-form-with-css-3--net-9542 Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486329 Share on other sites More sharing options...
cyberRobot Posted July 28, 2014 Share Posted July 28, 2014 The email address isn't being used since it's enclosed with single quotes: $email_from = '$visitor_email \r\n"'; Variables within strings need to be enclosed in double quotes: $email_from = "$visitor_email \r\n"; Also, your form doesn't have a field named "message", so $_POST['message'] isn't going to contain anything. You have a <textarea> called "text" though. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486337 Share on other sites More sharing options...
cyberRobot Posted July 28, 2014 Share Posted July 28, 2014 Fatal error: Call to undefined function IsInjected() in /home/public_html/www.2d2f.com/form-to-email.php on line 11 Now I get this IsInjected() is a user-defined function. You'll need to define the function before PHP can use it. Note that PHP has a built-in function for validating email addresses: http://php.net/manual/en/filter.examples.validation.php Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486340 Share on other sites More sharing options...
2d2f Posted July 28, 2014 Author Share Posted July 28, 2014 Thanks for your time. I removed the validation, and it works now, the quotes were missing and renaming the textarea helped. Can't get the phone to work. I ll try to figure out the rest. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486354 Share on other sites More sharing options...
cyberRobot Posted July 29, 2014 Share Posted July 29, 2014 Can't get the phone to work. Based on the code provided, the phone field isn't being used in the script which processes the form. It needs to be incorporated like you did with the name field. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486381 Share on other sites More sharing options...
2d2f Posted July 29, 2014 Author Share Posted July 29, 2014 (edited) Based on the code provided, the phone field isn't being used in the script which processes the form. It needs to be incorporated like you did with the name field. I tried it like this, and I don't know what I am doing wrong because I am not getting the input from the phone field: $name = $_POST['name']; $visitor_email = $_POST['email']; $phone = $_POST['phone']; $text = $_POST['text']; $email_from = "$visitor_email \r\n"; $email_subject = "New Form submission "; $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $text"; "Here is the phone:\n $phone"; <h6 class="phone"> <i class="icon-phone-sign"></i> <input type="text" name="phone" id="phone" /> <label for="phone">Phone</label> </h6> Edited July 29, 2014 by 2d2f Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486408 Share on other sites More sharing options...
Solution cyberRobot Posted July 29, 2014 Solution Share Posted July 29, 2014 To merge strings, you'll need to use the concatenation character. So this: <?php $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $text"; "Here is the phone:\n $phone"; ?> Needs to be changed to this: <?php $email_body = "You have received a new message from the user $name.\n" . "Here is the message:\n $text" . "Here is the phone:\n $phone"; ?> For what it's worth, I would actually go a step further. The following code is easier to understand and is less prone to mistakes: <?php $email_body = "You have received a new message from the user $name.\n"; $email_body .= "Here is the message:\n $text"; $email_body .= "Here is the phone:\n $phone"; ?> Note the ".=" operator. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486412 Share on other sites More sharing options...
2d2f Posted July 29, 2014 Author Share Posted July 29, 2014 Yes, it works now. I ll use your code I know that these are idiot questions for someone who is in good charge of the subject. I just want to thank you again for dedicating your time to help me. It can be marked as solved. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486418 Share on other sites More sharing options...
cyberRobot Posted July 29, 2014 Share Posted July 29, 2014 Yes, it works now. I ll use your code I know that these are idiot questions for someone who is in good charge of the subject. I just want to thank you again for dedicating your time to help me. It can be marked as solved. It's no problem at all. I would like to think that we've all been in your shoes at some point asking "idiot" questions. I know I've been there...and will be in the future. Quote Link to comment https://forums.phpfreaks.com/topic/290141-please-help-me-to-get-this-email-submit-form-working/#findComment-1486419 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.