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:
<?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:
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"
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 -- 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.
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
Try this:- foreach ($arr as $i => $t) { if ( empty($t) ) { unset($arr[$i-1]); } } $content = implode('', $arr); PHP:
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:
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..