add code for making required fields in php

Discussion in 'PHP' started by rcirone, Aug 25, 2009.

  1. #1
    Please help I am looking for a code and how to put it into my code listed below for making (Last name, First name, Phone, email required) if anyone can can help thank you all.


    <?php



    /* Subject and Email Variables */



    $emailSubject = 'Online Reservations';

    $WebMaster = 'anyone@gmail.com';



    /* Gathering Data Variables */



    $Last = $_POST['Last'];

    $First = $_POST['First'];

    $email = $_POST['email'];

    $phone = $_POST['phone'];

    $From = $_POST['From'];

    $To = $_POST['To'];

    $Pickupdate = $_POST['Pickupdate'];

    $pickuptime = $_POST['pickuptime'];

    $Numberofpeople = $_POST['Numberofpeople'];

    $adate = $_POST['adate'];

    $airline = $_POST['airline'];

    $flight = $_POST['flight'];

    $arrival = $_POST['arrival'];

    $ddate = $_POST['ddate'];

    $dairline = $_POST['dairline'];

    $dnumber = $_POST['dnumber'];

    $dtime = $_POST['dtime'];

    $OtherLocation = $_POST['OtherLocation'];

    $UniversalStudios = $_POST['UniversalStudios'];

    $SeaWorld = $_POST['SeaWorld'];

    $MagicKingdom = $_POST['MagicKingdom'];

    $EpcotCenter = $_POST['EpcotCenter'];

    $AnimalKingdom = $_POST['AnimalKingdom'];

    $HollywoodStudios = $_POST['HollywoodStudios'];

    $CirqueDuSoleil = $_POST['CirqueDuSoleil'];

    $DowntownDisney = $_POST['DowntownDisney'];

    $BuschGardens = $_POST['BuschGardens'];

    $SpecialRequirements = $_POST['SpecialRequirements'];

    $pickupaddress = $_POST['pickupaddress'];



    $body = <<<EOD



    Last Name: $Last

    First Name: $First

    E-Mail: $email

    Phone Number: $phone

    Transportation From: $From

    Transportation To: $To

    Pickup date: $Pickupdate

    Pickup Time: $pickuptime

    Number of People: $Numberofpeople

    Flight Arrival Date: $adate

    Arriving Airline: $airline

    Arriving Flight Number: $flight

    Flight Arrival time: $arrival

    Flight Departure Date: $ddate

    Departing Airline: $dairline

    Departing Flight Number: $dnumber

    Flight Departure Time: $dtime

    Universal Studios: $UniversalStudios

    Sea World: $SeaWorld

    Magic Kingdom: $MagicKingdom

    Epcot Center: $EpcotCenter

    Animal Kingdom: $AnimalKingdom

    Hollywood Studios: $HollywoodStudios

    Cirque Du Soleil: $CirqueDuSoleil

    Downtown Disney: $DowntownDisney

    Busch Gardens: $BuschGardens

    Other Location: $Other

    Special Requirements: $SpecialRequirements

    Pickup Address: $pickupaddress

    EOD;



    $headers = "From: $email\r\n";

    $headers = "Content-type: text/html\r\n";

    $success = mail($WebMaster, $emailSubject, $body, $headers);



    header('Location: confirmation.html');

    exit();
     
    rcirone, Aug 25, 2009 IP
  2. GreenWithEnvy

    GreenWithEnvy Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #2
    It's fairly easy. All you need to do is write an if statement for each one that you want required.

    if ($lastname == '') {
    //Last name was empty, and it is required
    die('Last name is required. <a href="javascript:history.back();">Go back</a>');
    }

    Or something like that. It's really up to you how you want to handle a missing variable.
     
    GreenWithEnvy, Aug 25, 2009 IP
  3. Izzl

    Izzl Banned

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    or a bit cleaner/easier


    if($firstname=="" || $lastname=="" || $email=="" ){
    die("<b><center>Please enter all required fields - <a href='form.php'>Back</a></center></b>");
    } else {
    echo "thanks $firstname $lastname for entering your email, $email";
    }
     
    Izzl, Aug 25, 2009 IP
  4. a53mp

    a53mp Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also, instead of doing $this = $_POST['that']; for everything.. you could also do
    extract($_POST);

    which would automatically assign all of the $_POST variables to itself so $_POST['this'] will automatically because $this

    you just have to make sure that it does not override any other variables.. it can also technically be a security issue, but it does save a lot of time and a lot of code.

    You could even loop through the $_POST if everything is required, and echo out the errors that way..

    so instead of

    if($firstname=="" || $lastname=="" || $email=="" ){
    die("<b><center>Please enter all required fields - <a href='form.php'>Back</a></center></b>");
    } else {
    echo "thanks $firstname $lastname for entering your email, $email";
    }

    you can do

    $error="";
    foreach ($_POST AS $var=>$val)
    {
    if (is_empty($val)){$error.="$var is required.<br>";}
    //or if($val==""){$error.="$var is required.<br>";}
    }

    if ($error!=""){echo $error;}

    and you could add more conditionals as needed to exclude variables or add formatting things..
     
    a53mp, Aug 25, 2009 IP