Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2012 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4384872 to your computer and use it in GitHub Desktop.
Save anonymous/4384872 to your computer and use it in GitHub Desktop.
Hello. I have a contact form that works via PHP. I want to update the code to make all fields REQUIRED. So that if someone leaves a text box empty and clicks 'SEND', a red REQUIRED will appear in the empty text field, and not submit the form. Below is the HTML and PHP.
HTML:
<form action="send_email.php" method="post" id="contact-form">
<div class="contact-form-input contact-input-wrapper">
<label class="contact-label">Name:</label>
<input type="text" name="name" class="required" />
</div>
<div class="contact-form-input contact-input-wrapper">
<label class="contact-label">E-mail:</label>
<input type="text" name="email" class="required email" />
</div>
<div class="contact-form-textarea contact-input-wrapper">
<label class="contact-label">Letter:</label>
<textarea name="question" rows="" cols="" class="required"></textarea>
</div>
<input type="submit" name="Submit" value="Send" class="button send-button">
<div class="clear"></div>
</form>
HERE'S THE PHP:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$question = htmlentities($_POST['question']);
// set here
$subject = "HAYLEYHENDRIX.com - Letter From Site.";
$to = "email@gmail.com";
$body = <<<HTML
Name: &nbsp;&nbsp;&nbsp; $name <br />
Email: &nbsp;&nbsp;&nbsp; $email <br />
Message: &nbsp;&nbsp;&nbsp; $question
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// Shows is email was sent.
echo "<h2>Email Sent Successfully</h2><p>I will be in touch within the next 24-48 hours. Thanks for reaching out to me.</p>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment