Simple PHP If statement help (ignore on email send)

Discussion in 'PHP' started by Kayz, Jun 9, 2009.

  1. #1
    Hi guys i have a pretty large working form, when the user dosent fill in a field and submits the form i still receive the field name(s) with a blank entry.. for a large form its a lot to look at when it arrives to see what has been filled in and what hasnt.

    Because it is a large form, some areas will be filled in and others will be ignored. What i want is the form to send me only those fields that have been filled in.



    This is my "example" working processor, i know it requires a simple if statement, but ive tried many without much luck, your help would be much appreciated.

    Cheers





    
    <?php 
    
    session_start(); 
    
    $receiver = '******@hotmail.com';
    $subject = 'Website - Feedback';
    $headers = 'info@************';
    
    
    $err_msg = '';
    if((!$name)){
            $err_msg = '<br />The following fields are missing, please insure you have filled out each field <br /><br />';
    
       if(!$name){
            $err_msg .= "* Your Name<br />";
        }
       include 'index.php';
    
     exit();
    }
    
    
    $arr= array();
    
    	$arr[0] = "\n\n Name: ";
    	$arr[1] = $_SESSION['name'];
    
    	$arr[2] = "\n\n email_address: ";
    	$arr[3] = $_SESSION['email_address'];
    
    
    	$arr[4] = "\n\n membership_type: ";
    	$arr[5] = $_SESSION['membership_type'];
    
    
    	$arr[6] = "\n\n terms_and_conditions: ";
    	$arr[7] = $_SESSION['terms_and_conditions'];
    
    	
    	$arr[8] = "\n\n name_on_card: ";
    	$arr[9] = $_POST['name_on_card'];
    
    
    
    	$arr[10] = "\n\n credit_card_number: ";
    	$arr[11] = $_POST['credit_card_number'];
    
    
    	$arr[12] = "\n\n credit_card_expiration: ";
    	$arr[13] = $_POST['credit_card_expiration_date'];
    
    	
    $content = ($arr[0] . $arr[1] . $arr[2] . $arr[3] . $arr[4] . $arr[5] . $arr[6] . $arr[7] . $arr[8] . $arr[9] . $arr[10] . $arr[11] . $arr[12] . $arr[13]);
    
    mail("$receiver", "$subject!", "$content", "From: $headers");
    header("Location: thankyou.html"); 
    
    ?>
    
    PHP:
     
    Kayz, Jun 9, 2009 IP
  2. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php 
    
    session_start(); 
    
    $receiver = '******@hotmail.com';
    $subject = 'Website - Feedback';
    $headers = 'info@************';
    
    
    $err_msg = '';
    if((!$name)){
            $err_msg = '<br />The following fields are missing, please insure you have filled out each field <br /><br />';
    
       if(!$name){
            $err_msg .= "* Your Name<br />";
        }
       include 'index.php';
    
     exit();
    }
    
    $arr= array();
    
        $arr[0] = "\n\n Name: ";
        $arr[1] = $_SESSION['name'];
    
        $arr[2] = "\n\n email_address: ";
        $arr[3] = $_SESSION['email_address'];
    
    
        $arr[4] = "\n\n membership_type: ";
        $arr[5] = $_SESSION['membership_type'];
    
    
        $arr[6] = "\n\n terms_and_conditions: ";
        $arr[7] = $_SESSION['terms_and_conditions'];
    
        
        $arr[8] = "\n\n name_on_card: ";
        $arr[9] = $_POST['name_on_card'];
    
    
    
        $arr[10] = "\n\n credit_card_number: ";
        $arr[11] = $_POST['credit_card_number'];
    
    
        $arr[12] = "\n\n credit_card_expiration: ";
        $arr[13] = $_POST['credit_card_expiration_date'];
    
    foreach ($arr as $t)
    {
    if (isset($t))
    {
    $content .= $t
    }
    mail($receiver, $subject!, $content, "From: $headers");
    header("Location: thankyou.html");
    }
    ?>
    
    PHP:
     
    SHOwnsYou, Jun 9, 2009 IP
  3. Kayz

    Kayz Active Member

    Messages:
    245
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you for your help, it returns me an error:


    "Parse error: syntax error, unexpected '}' on line 58"

    it is indicating to the curly bracket after the "$content .= $t"
     
    Kayz, Jun 9, 2009 IP
  4. calumk

    calumk Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $content = $t;
    PHP:
    replace with that
     
    calumk, Jun 9, 2009 IP
  5. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #5
    lol:)
    ....
    ....
     
    gapz101, Jun 9, 2009 IP
  6. Kayz

    Kayz Active Member

    Messages:
    245
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #6
    that does not work either
     
    Kayz, Jun 9, 2009 IP
  7. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    just put the semicolon after the t variable.
     
    ezprint2008, Jun 10, 2009 IP
  8. Kayz

    Kayz Active Member

    Messages:
    245
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Yes i tried this as shown above but fails :(

    Here are the other files that i have if this helps somebody:


    index.php

    <form method="post" action="form2.php"> 
    Name 
    <input type="text" name="name"> 
    Email 
    <input type="text" name="email_address"> 
    <input type="submit" value="Go To Step 2"> 
    </form> 
    
    PHP:
    form2.php

    <?php 
    
    //let's start the session 
    session_start(); 
    
    //now, let's register our session variables 
    session_register('name'); 
    session_register('email_address'); 
    
    //finally, let's store our posted values in the session variables 
    $_SESSION['name'] = $_POST['name']; 
    $_SESSION['email_address'] = $_POST['email_address']; 
    
    ?> 
    <form method="post" action="form3.php"> 
    membership_type 
    <input type="radio" group="membership_type" value="Free"> 
    <input type="radio" group="membership_type" value="Normal"> 
    <input type="radio" group="membership_type" value="Deluxe"> 
    terms_and_conditions 
    <input type="checkbox" name="terms_and_conditions"> 
    <input type="submit" value="Go To Step 3"> 
    </form>
    PHP:

    form3.php

    <?php 
    
    //let's start the session 
    session_start(); 
    
    //now, let's register our session variables 
    session_register('membership_type'); 
    session_register('terms_and_conditions'); 
    
    //finally, let's store our posted values in the session variables 
    $_SESSION['membership_type'] = $_POST['membership_type']; 
    $_SESSION['terms_and_conditions'] = $_POST['terms_and_conditions']; 
    
    ?> 
    <form action="form_process.php" method="post"> 
    name_on_card 
    <input type="text" name="name_on_card"> 
    credit_card_number 
    <input type="text" name="credit_card_number"> 
    credit_card_expiration_date 
    <input type="text" name="credit_card_expiration_date"> 
    <input type="submit" value="Submit"> 
    </form>
    PHP:

    form_processor.php

    <?php 
    
    //let's start our session, so we have access to stored data 
    session_start(); 
    
    $receiver = 'PUT IN YOUR EMAIL ADDRESS'; 
    $subject = 'Website - Feedback'; 
    $headers = 'FROM EMAIL ADDRESS'; 
    
    
    $err_msg = ''; 
    if((!$name)){ 
            $err_msg = '<br />The following fields are missing, please insure you have filled out each field <br /><br />'; 
    
       if(!$name){ 
            $err_msg .= "* Your First Name<br />"; 
        } 
       include 'index.php'; 
    
    exit(); 
    } 
    
    
    $arr= array(); 
    
        $arr[0] = "\n\n Name: "; 
        $arr[1] = $_SESSION['name']; 
    
        $arr[2] = "\n\n email_address: "; 
        $arr[3] = $_SESSION['email_address']; 
    
    
        $arr[4] = "\n\n membership_type: "; 
        $arr[5] = $_SESSION['membership_type']; 
    
    
        $arr[6] = "\n\n terms_and_conditions: "; 
        $arr[7] = $_SESSION['terms_and_conditions']; 
    
         
        $arr[8] = "\n\n name_on_card: "; 
        $arr[9] = $_POST['name_on_card']; 
    
    
    
        $arr[10] = "\n\n credit_card_number: "; 
        $arr[11] = $_POST['credit_card_number']; 
    
    
        $arr[12] = "\n\n credit_card_expiration: "; 
        $arr[13] = $_POST['credit_card_expiration_date']; 
    
         
    $content = ($arr[0] . $arr[1] . $arr[2] . $arr[3] . $arr[4] . $arr[5] . $arr[6] . $arr[7] . $arr[8] . $arr[9] . $arr[10] . $arr[11] . $arr[12] . $arr[13]); 
    
    
    mail("$receiver", "$subject!", "$content", "From: $headers"); 
    header("Location: thankyou.html"); 
    
    ?>
    PHP:
     
    Kayz, Jun 10, 2009 IP
  9. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #9
    In SHOwnsYou's code:-

    Find:

    if (isset($t))
    PHP:
    Replace With:

    if (!empty($t))
    PHP:
     
    clarky_y2k3, Jun 10, 2009 IP
  10. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Kayz --

    Do not rely on session_register or session_is_registered. These two functions will be deprecated in future php releases and will require a lot of work when your host upgrades. Instead, start phasing them out now to save yourself a headache earlier.

    Calumk and Gapz101 -- You have to have use .= in the code to add to $content, or else you're just replacing $content each time with the next line.
     
    SHOwnsYou, Jun 10, 2009 IP
  11. Kayz

    Kayz Active Member

    Messages:
    245
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #11
    i have 7 fields...

    trying the following spams my inbox several times sending me each individual field in each email and also the blank field still arrives.

    I have also tried changing it to: if (!empty($t)) and get the same result spamming my inbox.

    
    foreach ($arr as $t){if (isset($t)){$content .= $t}mail($receiver, $subject!, $content, "From: $headers");header("Location: thankyou.html");}
    
    PHP:

    Sigh never thought something like this will be so complicated..

    Anybody else who might know a better solution?

    Cheers
     
    Kayz, Jun 10, 2009 IP
  12. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #12
    Try this:-
    
    foreach ($arr as $i => $t)
    {
        if ( empty($t) )
        {
            unset($arr[$i-1]);
        }
    }
    
    $content = implode('', $arr);
    
    PHP:
     
    clarky_y2k3, Jun 10, 2009 IP
  13. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Kayz - your final bracket is out of place.

    Try this and tell us what it does...

    
    <?php
    
    session_start();
    
    $receiver = '******@hotmail.com';
    $subject = 'Website - Feedback';
    $headers = 'info@************';
    
    
    $err_msg = '';
    if((!$name)){
            $err_msg = '<br />The following fields are missing, please insure you have filled out each field <br /><br />';
    
       if(!$name){
            $err_msg .= "* Your Name<br />";
        }
       include 'index.php';
    
     exit();
    }
    
    $arr= array();
    
        $arr[0] = "\n\n Name: ";
        $arr[1] = $_SESSION['name'];
    
        $arr[2] = "\n\n email_address: ";
        $arr[3] = $_SESSION['email_address'];
    
    
        $arr[4] = "\n\n membership_type: ";
        $arr[5] = $_SESSION['membership_type'];
    
    
        $arr[6] = "\n\n terms_and_conditions: ";
        $arr[7] = $_SESSION['terms_and_conditions'];
    
       
        $arr[8] = "\n\n name_on_card: ";
        $arr[9] = $_POST['name_on_card'];
    
    
    
        $arr[10] = "\n\n credit_card_number: ";
        $arr[11] = $_POST['credit_card_number'];
    
    
        $arr[12] = "\n\n credit_card_expiration: ";
        $arr[13] = $_POST['credit_card_expiration_date'];
    
    foreach ($arr as $t)
    {
    if (!empty($t))
    {
    $content .= $t;
    }
    }
    mail($receiver, $subject!, $content, "From: $headers");
    header("Location: thankyou.html");
    ?>
    
    PHP:
     
    SHOwnsYou, Jun 10, 2009 IP
  14. Kayz

    Kayz Active Member

    Messages:
    245
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #14
    BRAVO!

    thanks man this works!! Your a star!! Many thanks!


    thanks to SHOwnsYou also for your help, i tried your method first but it did not work..
     
    Kayz, Jun 10, 2009 IP