Register

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
    Posts
    13
    Member #
    52369
    Liked
    1 times

    Making the Fields required in a Contact Form?

    Hi,

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

    <?php
    if(isset($_POST['submit'])) {

    $myemail = "xxxx@xxxxxx.com";
    $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:

    <?php
    // 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">
    <tr>
    <td valign="top" align="right">Subject:</td>
    <td>
    <input type="text" name="subject" size="35">
    <span class="error"><?php echo $subjectErr;?></span>
    </td>
    </tr>
    <tr>
    <td valign="top" align="right">Your Name:</td>
    <td>
    <input type="text" name="name" size="35">
    <span class="error"><?php echo $nameErr;?></span>
    </td>
    </tr>
    <tr>
    <td valign="top" align="right">Email:</td>
    <td>
    <input type="text" name="email" size="35">
    <span class="error"><?php echo $emailErr;?></span>
    </td>
    </tr>
    <tr>
    <td align="right">Your Message:</td>
    <td>
    <textarea name="message" rows="5" cols="40"></textarea>
    <span class="error"><?php echo $messageErr;?></span>
    </td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="Send" name="submit">
    <input type="reset" value="Reset" name="reset"></td>
    </tr>
    </table>
    </form>

    Anyone got any advice? Thanks.

  2.  

  3. #2
    Senior Member Fireproofgfx's Avatar
    Join Date
    Apr 2012
    Location
    Washington
    Posts
    840
    Member #
    31498
    Liked
    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 formspree.io 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="https://formspree.io/youremail@mail.com" >


    <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>

    <label><strong>Address</strong></label>
    <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>


    <label>&nbsp;</label>
    <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: PurevB.com