If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Oct 2015
    Member #
    1 times

    Making the Fields required in a Contact Form?


    I'm trying to combine the two php codes such the following contact e-mail gets sent:

    if(isset($_POST['submit'])) {

    $myemail = "";
    $subject = $_POST['subject'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $headers = "From:Contact Form <$myemail>\r\n";
    $headers .= "Reply-To: $name <$email>\r\n";

    echo "Your message has been sent successfully!";
    mail($myemail, $subject, $message, $headers);

    } else {

    echo "An error occurred during the submission of your message";


    Only if there are no missing fields in the form below:

    // define variables and set to empty values
    $subjectErr = $nameErr = $emailErr = $messageErr = "";
    $subject = $name = $email = $message = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["subject"])) {
    $subjectErr = "Subject is missing";
    } else {
    $subject = test_input($_POST["subject"]);

    if (empty($_POST["name"])) {
    $nameErr = "Name is missing";
    } else {
    $name = test_input($_POST["name"]);

    if (empty($_POST["email"])) {
    $emailErr = "Email is missing";
    } else {
    $email = test_input($_POST["email"]);

    if (empty($_POST["message"])) {
    $messageErr = "Message is missing";
    } else {
    $message = test_input($_POST["message"]);

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;

    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* All fields are required</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <table cellspacing="5" cellpadding="5" align="center">
    <td valign="top" align="right">Subject:</td>
    <input type="text" name="subject" size="35">
    <span class="error"><?php echo $subjectErr;?></span>
    <td valign="top" align="right">Your Name:</td>
    <input type="text" name="name" size="35">
    <span class="error"><?php echo $nameErr;?></span>
    <td valign="top" align="right">Email:</td>
    <input type="text" name="email" size="35">
    <span class="error"><?php echo $emailErr;?></span>
    <td align="right">Your Message:</td>
    <textarea name="message" rows="5" cols="40"></textarea>
    <span class="error"><?php echo $messageErr;?></span>
    <td><input type="submit" value="Send" name="submit">
    <input type="reset" value="Reset" name="reset"></td>

    Anyone got any advice? Thanks.


  3. #2
    Senior Member Fireproofgfx's Avatar
    Join Date
    Apr 2012
    Member #
    170 times
    Lately I have been having a lot of issue with my php form that is similar to yours whether the email servers don't except the submissions or I receive more spam/fake submissions than actual form submissions. So I came across which allows you to run forms through them for free, and they filter the spam form submissions. All I do is code the form Html and then add the Action like below and they take care of the rest. I am not sure if this is something that would interest you but I find it super convenient and maybe you will too.

    <form method="post" action="" >

    <label for="name"><strong>Name</strong></label>
    <input type="text" name="Name" id="name" placeholder="Your Name" required> <!-- Add Required to form to not allow browser to send from unless this is filled out-->

    <label for="email"><strong>Email</strong></label>
    <input type="text" name="Email" id="email" placeholder="Your Email" required>

    <input type="text" name="Address" id="address" placeholder="Your Address">

    <label><strong>How can we help you?</strong></label>
    <textarea name="Message" placeholder="Your Message"></textarea>

    <input type="submit" value="Send"></form>

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 08:17 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: