Sunday, March 6, 2011

Sunday 3/6/11

Just for fun I am going to put here the entire code (so far) for the process form php


<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$phoneNumber = $_POST['phoneNumber'];
$emailAddress = $_POST['emailAddress'];
$contactMethod = $_POST['contactMethod'];
$returnString = "requestContact.php";

$dataArray = array($firstName, $lastName, $phoneNumber, $emailAddress, $contactMethod);
$errorArray = array();


// This function will build a php url that will outline to the contactAdministration PHP form all the errors.
function buildErrorString($returnString, $dataArray, $errorArray) {
$returnString = $returnString . "?";
//use a foreach loop


for ($i=0; $i<count($dataArray); $i++) {
if ($dataArray[$i] != "") {
$name = "value" . $i;
$returnString = $returnString . $name . "=" . $dataArray[$i] . "&";
}
}

foreach ($errorArray as $value) {
$returnString = $returnString . $value . "=" . " Required" . "&";
}

return $returnString;
} //end function buildErrorString


// This function will deposit the data into the notepad file
function depositData($dataArray){
// getCwd() gets the current directory, useful for building the file stream pointer
$current_directory = getCwd();
$file_address = $current_directory . "/files/reqContact.txt";
$outputString = $dataArray[0] . "\t" . $dataArray[1] . "\t" . $dataArray[2] . "\t" . $dataArray[3] . "\t" . $dataArray[4] . "\n";
$fileOpen = fopen($file_address, 'a');
// frwrite() writes the contents of the outputString to the file stream pointed to by the $fileOpen handle
fwrite($fileOpen, $outputString, strlen($outputString));
fclose($fileOpen);


} //end function depositData

for ($i=0; $iif ($dataArray[$i] == "") {
$errorArray[] = "blank" . $i;
}
}



if (count($errorArray) > 0) {
$returnString = buildErrorString($returnString, $dataArray, $errorArray);
} else {
depositData($dataArray);
}

// strips off any extra ampersand off the end of the return string
$returnString = rtrim($returnString, "&");

header("location: $returnString");


?>

No comments: