dcsimg
www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Contact Form is not sending all the information

  1. #1
    Join Date
    Feb 2005
    Posts
    87

    Contact Form is not sending all the information

    Contact Form is not sending '$message', '$email', or '$name', just '$from' info arrives
    Here's the code:

    Code:
    <?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";
    			 $email = $_POST['email'];
    			 $name = $_POST['name'];
    			 $message = $_POST['message'];
    			 
    			 if (mail($to, $message, $from, $email, $name )){
    			 echo "Thank You. Your message has been sent.";
    			 } else {
    			 echo "An error has occurred and your message could not be sent.";
    			 }
    			?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,430
    Everything you want in the actual message body of the email needs to be in the 3rd argument to mail (see http://php.net/mail ):
    bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
    Therefore you'll want to append those values to the $message variable in your code, as opposed to making them additional arguments to mail() -- and that then needs to be the 3rd argument, not the second (which is the subject value). So it might be something like:
    PHP Code:
    $message "Name: {$_POST['name']}\r\nEmail: {$_POST['email']}\r\n\r\n{$_POST['message']}";
    if (
    mail($to"Customer Inquiry"$message$from)) { 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Feb 2005
    Posts
    87
    Thank you for your reply. I've added in your suggestions like so:

    Code:
    <?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";
    $email = $_POST['email'];
    $name = $_POST['name'];
    $message = $_POST['message'];
    
    $message = "Name: {$_POST['name']}\r\nEmail: {$_POST['email']}\r\n\r\n{$_POST['message']}";
    
    if (mail($to, "Customer Inquiry", $message, $from)) {
    
    echo "Thank You. Your message has been sent.";
    } else {
    echo "An error has occurred and your message could not be sent.";
    }
    ?>
    And now I see this in the email that arrives:

    Name:
    Email:

    but, no info that was entered into a Form field.
    Any additional help as to how to show Form field info, will be appreciated.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,430
    First step would be to make sure that $_POST has the expected field names. (No guarantee my guesses were correct. ). A quick and dirty check just for debugging somewhere near the top of the script:
    PHP Code:
    die("<pre>".print_r($_POST1)."</pre>"); 
    Remember that those array keys/field names are case-sensitive.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Feb 2005
    Posts
    87
    Much thanks again for your reply/help.

    I looked and believe the field name match the php.

    I added your debug code suggestion and see this:

    Code:
    <pre>Array ( ) </pre>
    Here is more Form code:


    [CODE]
    <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.r
    Last edited by chrisjchrisj; 10-19-2016 at 05:12 PM.

  6. #6
    Join Date
    Feb 2005
    Posts
    87
    <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>

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,430
    So, that empty array in the debug output is telling me nothing got sent to the PHP script. Being primarily a server-side dev, I'll leave it to someone else to wade through the JavaScript and figure out why it's not sending anything. (Personally, I'd have just used a HTML form, but that's just me.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  8. #8
    Join Date
    Feb 2005
    Posts
    87
    Thanks for your reply.
    I began with an html form, but could not figure out how to stay on the same page after 'submit', and show the 'sent message'

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    If your html form uses php vars in its value attibutes AND if your php script retrieves those same php var values from the POST input then your process can simply do its processing and re-send the same html page back (if necessary), with those same php vars in the value attributes, along with a message field. I do it all the time.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  10. #10
    Join Date
    Feb 2005
    Posts
    87
    Thanks for your message.
    I'm not sure if it's doing all those things.

    Here's the Form code. Any help will be appreciated:

    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>

  11. #11
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    You need to add the value attributes to your inputs. Then in you script when you grab those values from $_POST, store them in those same php vars that you use in the value attributes.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  12. #12
    Join Date
    Feb 2005
    Posts
    87
    Thanks for your reply. Greatly appreciated.
    Regarding "add the value attributes to your input". I'm not clear on that.

    Aren't I already doing that with:
    Code:
     name="name"
    name="email"
    name="message"
    and this?

    Code:
    $email = $_POST['email'];
    $name = $_POST['name'];
    $message = $_POST['message'];
    I guess I don't follow what you're saying.
    Any clarification will be appreciated.

  13. #13
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    Close. What you are showing is the name attributes of your html. You need to add the value ones. And those values should then use the php vars that you are saving in the php code you showed.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  14. #14
    Join Date
    Feb 2005
    Posts
    87
    Thank you for your reply.
    Can you provide an example of what you mean, please?

  15. #15
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    Do you not have any resource for looking up an html tag?

    Code:
    <input type='text' name='input1' value='$input1'>";
    This allows you to output the tag with the value of $input1 displayed in it.

    PHP Code:
    $input1 $_POST['input1']; 
    This is how you get the value of the tag.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles