Code for required fields in PHP e-mail form

Discussion in 'PHP' started by jasonstine, Jul 23, 2009.

  1. #1
    Please help -- this may be easy for you, hard for me:

    Simply want to add code for making required fields in PHP e-mail form so that the user can ONLY SUBMIT the form after filling-in all fields.

    Here's the code, please tell me how/where to add the code for making required fields (first name, last name, company, e-mail address, Web site, telephone, industry) --
    =====


    <?
    $first_name = $_REQUEST['first_name'] ;
    $last_name = $_REQUEST['last_name'] ;
    $company = $_REQUEST['company'] ;
    $emailaddress = $_REQUEST['email'] ;
    $website = $_REQUEST['website'] ;
    $textfield = $_REQUEST['textfield'] ;
    $telephone = $_REQUEST['telephone'] ;
    $industry = $_REQUEST['industry2'] ;
    $message = $_REQUEST['message'] ;
    $message = "$message\n\n$first_name $last_name\n$company\n$industry\n$website\n\n$name\n$textfield $telephone";mail( "to@domain.com", "Company Interest", $message, "From: $emailaddress" );
    ?>
    <script>
    <!--
    window.location= "thankyou.html"
    //-->
    </script>
    <?php
    exit;?>


    =====
    Thank you very much.
     
    jasonstine, Jul 23, 2009 IP
  2. Aces A

    Aces A Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $validForm = true;

    if(strlen($first_name) < 1 ) $validForm = false;
    .
    .
    .
    if(strlen($message) < 1 ) $validForm = false;

    if($validForm) //send mail
    else //do something else
     
    Aces A, Jul 23, 2009 IP
  3. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #3
    You can do this using javascript on the page where the actual form is displayed. In that case, your verification of user inputs are done before form submission.

    Your form will look like below..

    ============

    <form action="thankyou.php" method="post" name="form1" onsubmit="return checkform(this);">
    <input name="name" type="text" id="name" value="<?php echo $_GET['name'];?>"/>
    <input name="phone" type="text" id="phone" value="<?php echo $_GET['phone'];?>"/>
    <input name="email" type="text" id="email" value="<?php echo $_GET['email'];?>"/>
    <input name="Submit" type="submit" value="Submit"/>
    </form>

    ============

    You should code the javascript in the <head> tags of the page...see below

    ============

    function checkform(form)
    {
    n = form.name;
    p = form.phone;
    e = form.email;

    if(n.value=='' || n.value.length<2)
    {
    alert("Please Enter Full Name");
    n.focus();
    return false;
    }
    else if(p.value=='' || checknumber(p) || p.value.length<6)
    {
    alert("Please Enter valid Contact Number");
    c.focus();
    return false;
    }
    else if(e.value==''|| !checkemail(e.value))
    {
    alert("Please Enter valid Email Address");
    e.focus();
    return false;
    }
    else return true;
    }

    ============
    You should also create two functions checknumber(num) and checkemail(text) to validate number input and correct email address format.

    using this, when user submits the form, it checks for correct input, otherwise shows alerts on browser.
     
    spc, Jul 24, 2009 IP
  4. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    I would have to say for serious security reason, both on-page verification (javascript) and server-side verification is required. Validating input data will be needed for extra care.

    For server-side, $_REQUEST is not popular nowadays. If you may want to specific whether it is $_GET or $_POST. Use isset() if necessary.
     
    zandigo, Jul 24, 2009 IP