ethanfox27 ethanfox27 - 2 months ago 8
HTML Question

Certain FormFiled not in body of email

Ok so I have this form made out, and it is set to go to my email. The form sends, but it only shows the bottom two fields in the emails. As you can see in the code below there are 5 fields. How can i make to where I recvieve the info for all of the field?

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.";
}

?>

Answer

Do you see the missing concatenates/dots in here?

// 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";

That broke "the chaining".

Add them:

// 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";
Comments