laffin Posted May 1, 2009 Share Posted May 1, 2009 Let me say, Holy.....l Thats a big script a lot of redundancy, which could have been solved with an array of values <?php $formvars = array( 'Personal' => array( array('Name',true,'Name'), array('Date',true,'Date'), array('DOB',true,'DOB'), array('Age',true,'Age'), array('Weight',true,'Weight') ), 'Current Medications' => array( array('CurrentMed1',false,'#1'), array('CurrentMed2',false,'#2'), array('CurrentMed3',false,'#3'), array('CurrentMed4',false,'#4'), array('CurrentMed5',false,'#5'), array('CurrentMed6',false,'#6'), array('CurrentMed7',false,'#7'), array('CurrentMed8',false,'#8'), array('CurrentMed9',false,'#9'), array('CurrentMed10',false,'#10'), array('CurrentMed11',false,'#11') ), 'Medication Allergies' => array( array('MedicationAllergies1',false,'#1'), array('MedicationAllergies2',false,'#2'), array('MedicationAllergies3',false,'#3'), array('MedicationAllergies4',false,'#4') ), ); $fail=false; foreach($formvars as $section=>$items) { $list=array(); foreach($items as $item) { if($item[1] && (!isset($_POST[$item[0]]) || empty($_POST[$item[0]])) { $fail=true; $error="'{$item[0]}' is required"; break; } if(isset($_POST[$item[0]]) && !empty($_POST[$item[0]]) $list[]="{$item[2]}: " .addslashes($_POST[$item[0]]); } if($fail==false) break; if(!empty($list)) { $body.="{$section}:\n" . implode("\n\t",$list) . "\n\n"; } } if($fail) { die("Error: {$error}"); } ?> This should put each var into its own section like Personal: Name: Laffin Date: 4/30/09 and skip sections and items that are empty although I would have suggested using arrays for some of them form vars, such as Current Medication. But that ya can do later. This was its pretty easy adding new variables to your form and processing is a breeze the arrays are in format 'Section Name' => array( array('POST_VAR',Required,'Header Name (used in body of message)') ) Okay that nuff for me Quote Link to comment https://forums.phpfreaks.com/topic/156121-php-form-help/page/2/#findComment-823762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.