Chrisj Posted October 22, 2016 Share Posted October 22, 2016 (edited) When 'submit' is selected in this Contact Form, the 'message' field info is the only thing that sends/arrives. Someone suggested it may have to do with the 'mail command', but any help with getting the 'name' and 'email' field info to send will be appreciated. <?php $data = json_decode(file_get_contents("php://input")); $name = trim($data->name); $name = str_replace(array("\r", "\n"), array(" ", " "), $name); $email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL); $message = trim($data->message); if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "One or more invalid entries. Please try again."; exit; } $to = "support@...com"; $from = "From: contact-form@...com". "\r\n"; if (mail($to, "Customer Inquiry", $message)) { echo "Thank You. Your message has been sent."; } else { echo "An error has occurred and your message could not be sent."; } ?> Here's more code: <form id="ajax-contact" method="post"> <table class="table10"> <tr> <td> <label for="name">Your Name:</label> <input id="contact-name" type="text" name="name" required> </td> </tr> </tr> <tr> <td> <label for="email">Your Email Address:</label> <input id="contact-email" type="email" name="email" required> </td> </tr> <tr> <td> <label for="message">Message:</label> <textarea id="contact-message" type="text" name="message" required></textarea> </td> </tr> <tr> <td> <button type="submit">Send</button> </td> </table> </form> <div id="form-response"></div> <script> $(function () { var form = $("#ajax-contact"); form[0].reset(); form.submit(function (event) { event.preventDefault(); var data = { "name": $("#contact-name").val(), "email": $("#contact-email").val(), "message": $("#contact-message").val() }; $.ajax({ url: "../contact_form_handle.php", data: JSON.stringify(data), contentType: "application/json; charset=utf-8", dataType: "text", type: "POST"}) .done(function (response) { $("#form-response").text(response); form[0].reset(); }) .fail(function (data) { if (data.responseText.length) { $("#form-response").text(data.responseText); } else { $("#form-response").text("An error has occurred and your message could not be sent."); } }) }); }); </script> Edited October 22, 2016 by Chrisj Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 22, 2016 Share Posted October 22, 2016 I'm kind of old school, and like to break it into pieces. Your problem is either html, js, php, networking, etc, etc. Start ruling things out. In your server script, add var_dump($_POST). What do you get? And why are you using file_get_contents("php://input") instead of the superglobal $_PRINT? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 22, 2016 Share Posted October 22, 2016 (edited) You are only sending the message as the e-mail content, so how exactly do you expect to receive the name and e-mail address? PHP only does what you tell it to do. You either have to actually put the contact data into the e-mail content, e. g.: name: <insert name here> e-mail address: <insert address here> message: <insert message here> Or you add them to a Reply-To header so that they show up in your e-mail program. In any case, the mail() function sucks and is going to cause security problems as soon as you start adding dynamic headers. Use a proper mailer library like PHPMailer. The whole JSON stuff doesn't make a lot of sense either. You can skip all the encoding and decoding if you use plain old default form encoding. Edited October 22, 2016 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Chrisj Posted October 22, 2016 Author Share Posted October 22, 2016 Thanks for your replies. This is not code that I wrote. Someone provided it to me and it seems like it's close to working. I'm not familiar with "superglobal $_PRINT". And I'm not clear on "put the contact data into the e-mail content", doesn't the Contact Form user enter the content into the Form fields? And additional guidance/examples, would be appreciated Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 22, 2016 Share Posted October 22, 2016 The user fills out the contact form and submits three pieces of data: The username, the e-mail address and the message. What you do with this data is your own responsibility. Right now, you're only using the submitted message as the e-mail content. Everything else gets dropped. It doesn't show up anywhere. If you haven't written the code, you'll at least need to read and understand it. Otherwise there isn't really much we could do. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 22, 2016 Share Posted October 22, 2016 I'm not familiar with "superglobal $_PRINT". Sorry, meant $_POST. My point is to break your application up and test each part. Quote Link to comment Share on other sites More sharing options...
Chrisj Posted October 23, 2016 Author Share Posted October 23, 2016 Thanks for your replies. Any additional help will be appreciated. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 23, 2016 Share Posted October 23, 2016 Thanks for your replies. Any additional help will be appreciated. You said: When 'submit' is selected in this Contact Form, the 'message' field info is the only thing that sends/arrives. What does that mean to you? It means to me... First, I should do one more (or even more) reality checks of what the client is sending to the server to ensure what I am witnessed is reality. 80% of the time it isn't. Assuming 1 passes, this has nothing to do with the server (unless it is supplying faulty HTML), and I should be looking at the client (i.e. the form and/or js). I shouldn't be going down a rabbit hole troubleshooting the email class as it is not the issue, but should focus solely on the client. That I should definitely vote against Trump as the script will not run correctly if he is elected. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 23, 2016 Share Posted October 23, 2016 Guys, read the code: $data = json_decode(file_get_contents("php://input")); // ... $message = trim($data->message); // ... mail($to, "Customer Inquiry", $message); How exactly are the name and e-mail address supposed to get to the receiver? They're not in the e-mail content, they're not in any header, they're just discarded in the script. PHP doesn't magically add data to e-mails. You have to actually do it yourself, and I've already explained 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.