1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Contact Form field info doesn't send/arrive

Discussion in 'PHP' started by chrisj, Oct 19, 2016.

  1. #1
    When i select 'submit' for this Contact Form, field info doesn't send/arrive.
    All I see when the email arrives is Name: and Email:

    <?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.";
    }
    ?>
    Code (markup):
    Any help with getting the filled-in Form field information to be captured/sent, will be appreciated.
     
    chrisj, Oct 19, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're reusing the $_POST-variables a lot, while simultaneously attaching them to named variables, and also, what's the point of doing all that validating and stuff up top, when you don't use any of those variables later?

    Regardless, without seeing the actual form you're fetching these values from, it's hard to help. I'm guessing that there is something in the form that isn't up to scratch.
     
    PoPSiCLe, Oct 19, 2016 IP
  3. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #3
    From the looks of it
    $_POST['message'] is probably empty. You may have made a typo in your form
     
    Einheijar, Oct 20, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    It's not just the message that is empty, the name as well - which either means the POST-variables aren't there (wrong code in the form), or there is another issue (which might be visible in the PHP-error-log).
     
    PoPSiCLe, Oct 20, 2016 IP
  5. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thanks for your replies. This is not my code. Someone else provided it to help me and I'm just trying to get it to work.

    Currently I removed these lines from the php code:
    $email = $_POST['email'];
    
    $name = $_POST['name'];
    $message = $_POST['message'];
    $message = "Name: {$_POST['name']}\r\nEmail: {$_POST['email']}\r\n\r\n{$_POST['message']}";
    Code (markup):
    So, now it looks like this:

    <?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.";
    }
    ?>
    Code (markup):
    And now it successfully sends the info that is entered into the 'message' field. If I can get some help in how to get the info entered into the 'name' and 'email' fields to send/arrive, I'd greatly appreciate any assistance.

    The person that suggested I remove those lines said "don’t send POST vars".
    and "you are sending a JSON string". He suggested something to do with the mail command.
     
    Last edited: Oct 22, 2016
    chrisj, Oct 22, 2016 IP