If/Else Statement in PHP

Discussion in 'Scripts' started by Hcp4, Feb 24, 2012.

to remove all ads.
  1. Hcp4

    Hcp4 New Member

    Messages:
    1
    I need to place an If/Else statement (or something similar) in a PHP script file I have, but am having trouble. I'm relatively new at Web design so please be specific; I think this is a pretty easy fix, I just am not getting the code right.

    My PHP file is taking information constituents fill out in an online form, and sends the info to someone through an e-mail. Here is what my PHP file looks like (sections of it):

    $DLocation1 = $_REQUEST['DLocation1'];
    $DAddress1 = $_REQUEST['DAddress1'];
    $DCity1 = $_REQUEST['DCity1'];
    $DState1 = $_REQUEST['DState1'];
    $DZip1 = $_REQUEST['DZip1'];
    $DPhone1 = $_REQUEST['DPhone1'];
    $DTime1 = $_REQUEST['DTime1'];

    $DLocation2 = $_REQUEST['DLocation2'];
    $DAddress2 = $_REQUEST['DAddress2'];
    $DCity2 = $_REQUEST['DCity2'];
    $DState2 = $_REQUEST['DState2'];
    $DZip2 = $_REQUEST['DZip2'];
    $DPhone2 = $_REQUEST['DPhone2'];
    $DTime2 = $_REQUEST['DTime2'];


    // Email body content
    $content =

    .'First Stop: '.$DLocation1."\n"
    .'Address: '.$DAddress1."\n"
    .'City: '.$DCity1."\n"
    .'State: '.$DState1."\n"
    .'Zip Code: '.$DZip1."\n"
    .'Phone: '.$DPhone1."\n"
    .'Arrival Time: '.$DTime1."\n\n"

    .'Second Stop: '.$DLocation2."\n"
    .'Address: '.$DAddress2."\n"
    .'City: '.$DCity2."\n"
    .'State: '.$DState2."\n"
    .'Zip Code: '.$DZip2."\n"
    .'Phone: '.$DPhone2."\n"
    .'Arrival Time: '.$DTime2."\n\n"

    This works fine, but all of my text ("First stop", "Address", etc.) prints out for ALL of the fields (there are many more than this), even if the constituent does not fill out all of the fields in the form. So my client gets a loong e-mail with the unnecessary text.

    What I want is an If/Else statement to say: IF the "DLocation2" field has information, then place in the e-mail:
    .'Second Stop: '.$DLocation2."\n" If the textfield is blank, then don't submit anything and go on to the next field. And I'll do this for all of the fields.

    Can you help? I have to get this figured out in the next few days and other forums didn't know what to do!

    thanks!
     
  2. MarkR

    MarkR New Member

    Messages:
    105
    Something like:


    PHP:
    // Email body content
     
    $content '';

    if(isset(
    $_REQUEST['DLocation1']) && $_REQUEST['DLocation1'] != NULL)
     
    $content.= 'First Stop: '.$_REQUEST['DLocation1']."\n";
    Repeat..
     

Share This Page