n@e Posted February 27, 2010 Share Posted February 27, 2010 Hello!!! I have created a fields array to loop through a form and print the information into an email. However I need help ignoring optional information that may not be filled out by the user, for example the input boxes for access start/end date are optional and dont have to completed but the email that is created will still come through with "Access Start date: " blank........ Do I need another foreach loop to check if information has been submited? Your help is much appreciated $fields = array(); $fields{"First_Name"} = "First Name"; $fields{"Last_Name"} = "Last Name"; $fields{"Card"} = "Card Number"; $fields{"Old_Card"} = "Old Card Number"; $fields{"Description"} = "Description"; $fields{"Access_Start_Date"} = "Access Start Date"; $fields{"Access_End_Date"} = "Access End Date"; $fields{"Qty"} = "Quantity"; $fields{"Cost"} = "Cost Centre"; $fields{"Details"} = "Details"; $body = "We have received the following information from $_SESSION[username]:\n\n"; foreach($fields as $a => $b){ $part1 .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } Link to comment https://forums.phpfreaks.com/topic/193540-fields-array/ Share on other sites More sharing options...
trq Posted February 27, 2010 Share Posted February 27, 2010 Firstly, array indexes go within [] not {}. As for your question, I'm really not clear what your asking. Link to comment https://forums.phpfreaks.com/topic/193540-fields-array/#findComment-1018854 Share on other sites More sharing options...
alpine Posted February 27, 2010 Share Posted February 27, 2010 <?php $fields = array( 'First_Name' => 'First Name', 'Last_Name' => 'Last Name', 'Card' => 'Card Number', 'Old_Card' => 'Old Card Number' // and so on ); $body = "We have received the following information from $_SESSION[username]:\n\n"; foreach($_REQUEST as $a => $b){ if(array_key_exists($a, $fields) && !empty($b)){ $part1 .= sprintf("%20s: %s\n", $fields[$a], $b); } } ?> Link to comment https://forums.phpfreaks.com/topic/193540-fields-array/#findComment-1018867 Share on other sites More sharing options...
n@e Posted February 28, 2010 Author Share Posted February 28, 2010 Cheers alpine bud, works perfectly!!!! Link to comment https://forums.phpfreaks.com/topic/193540-fields-array/#findComment-1019242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.