某些FormFiled不在电子邮件正文中

时间:2016-09-10 20:53:21

标签: php html forms

好的,所以我已经制作了这个表格,并设置为我的电子邮件。表单发送,但它只显示电子邮件中的底部两个字段。正如您在下面的代码中看到的,有5个字段。我怎样才能找到我收集所有领域信息的地方?

HTML:

<form id="ajax-contact" method="post" action="../js/mailer.php">

                    <div class="field">                                         
                        <select id="select" name="interest" title="Interested in ..." class="selectpicker">
                            <option value="NA">Interested in...</option>
                            <option value="Website">Website</option>
                            <option value="Web Design"> Only Web Design</option>
                            <option value="Brand Consulting">Brand Consulting</option>
                        </select>
                    </div>

                    <div class="field">
                        <input  name="name" placeholder="Name:" type="text" id="name" required>
                    </div>

                    <div class="field">
                        <input name="email"  placeholder="Email: " type="email" id="email"required>
                    </div>
    <div class="field">
                    <div class="wrapper">
                        <label><p>Budget:</p></label>
                      <div id="radio" class="toggle_radio">
                        <input value="$4k" type="radio" class="toggle_option" id="first_toggle" name="budget">
                        <input value="$5k - $9k" type="radio" checked class="toggle_option" id="second_toggle" name="budget">
                        <input value="$10k+" type="radio" class="toggle_option" id="third_toggle" name="budget">

                        <label for="first_toggle"><p>>$4k</p></label>
                        <label for="second_toggle"><p>$5k - $9k</p></label>
                        <label for="third_toggle"><p>$10k+</p></label>
                        <div class="toggle_option_slider">
                        </div>
                      </div>
                    </div>  
    </div>

<div class="field">
            <textarea placeholder="project description(optional)" id="message" name="message" required></textarea>
</div>

<div class="field">
    <button id="button" type="submit">SEND REQUEST</button>
</div>
</form>

PHP:

<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $option = $_POST['interest'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $rdb_value = $_POST['budget'];
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "EMAIL GOES HERE";

    // Set the email subject.
    $subject = "New Request from: $name";

    // Build the email content.
    $email_content = "Interested in: $option\n";
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content = "Budget: $rdb_value\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

?>

1 个答案:

答案 0 :(得分:2)

你在这里看到缺少的连接点/点吗?

// Build the email content.
$email_content = "Interested in: $option\n";
$email_content  = "Name: $name\n";
               ^ there
$email_content .= "Email: $email\n\n";
$email_content  = "Budget: $rdb_value\n";
               ^ and there
$email_content .= "Message:\n$message\n";

这打破了“链条”。

添加它们:

// Build the email content.
$email_content = "Interested in: $option\n";
$email_content .= "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Budget: $rdb_value\n";
$email_content .= "Message:\n$message\n";