Jump to content

PHP Form Help


defpdp

Recommended Posts

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.