Jump to content

Fields array


n@e

Recommended Posts

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  :D

 

$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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.