Make Contact Form Fields Required

Discussion in 'HTML & Website Design' started by Glen, Sep 13, 2007.

  1. #1
    I have a very simple contact form in HTML which posts action to the mail.php page which contains the following code

    <?
    function checkOK($field)
    {
    if (eregi("\r",$field) || eregi("\n",$field)){
    die("Invalid Input!");
    }
    }
    
    $name=$_POST['name'];
    checkOK($name);
    $email=$_POST['email'];
    checkOK($email);
    $site=$_POST['site'];
    checkOK($site);
    $budget=$_POST['budget'];
    checkOK($budget);
    $comments=$_POST['comments'];
    checkOK($comments);
    
    $to="[I]HIDDEN FOR DP[/I]";
    $message="$name just filled in your Contact Form.
    Additional Info (optional): \n$comments\n\nTheir e-mail address is: $email 
    Their budget is $budget
    Their website is $site";
    if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
    echo "Thanks for Contacting us. ";
    } else {
    echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
    }
    ?>
    PHP:
    I would like to make some of the fields required as I keep getting blank emails, can anyone help?

    Cheers,
    Glen:)
     
    Glen, Sep 13, 2007 IP
  2. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
  3. kudy

    kudy Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You could change the checkOK function so it checks more than it is currently, like this:

    function checkOK($field)
    {
    if (eregi("\r",$field) || eregi("\n",$field) || $field == NULL){
    die("Invalid Input!");
    }
    }
    Code (markup):
    This should now check each field to see if it is blank, and if it is, it tells the user that the input is invalid. If you don't want all the fields to be checked, add another function that checks only whether a field is blank or not like this:

    function checkBlank($field)
    {
    if ($field == NULL){
    die("Invalid Input!");
    }
    }
    Code (markup):
    Tell me if this helps!
     
    kudy, Sep 15, 2007 IP