How do I create an array using a variable as the keys?
This is an excerpt of the code I'm making for a form that both creates and processes itself.
$submission = "value submitted by User through $_POST"; ie: Joe
$lable = "value of variable created by script manager"; ie: Name
The following code is assembled inside a foreach statement. Which may be my entire problem . what I'm trying to do is have all my variables run through this form and strore all errors in an array and print them separately from the printing of the form even though they are both in the same foreach statement. I have gotten the form to print fine but whenever I try to print the $errorArray or $cssErrors array the array prints this Array ( [0] => ) three times inbetween my form elements. I would be happy to e-mail the source of my code to anyone willing to filter though, I comment very well but the code it'self is 160 lines in one file, but includes a blank html doc. and lots of other lines that can be ignored.
if ($submission) {
if ($howToValidate=01) {// validate General ie: name address city state
if (ereg ("<|>|\\\\|,|:|\^|\]|\[|;", $submission ) ){//(ereg ("string", $date))
$cssErrors[$lable]=$error;
$formErrors=$formError+1;
$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
"'s cannot contain the following characters: < > \ , : ^ ] [ ;";
}
}
// this peice of crap won't let me post my code because it thinks there is a url in it // even though there isn't and I don't know why it thinks that but here is the php
Code:
<?php
if (!isset($_POST['Name@name%01-1']) ){
$_POST['Name@name%01-1']= "";
$_POST['E-MAIL@eMail%02-1']= "";
$_POST['Phone@phone%03-1'] = "";
}
$a = $_POST;
$numFormElements=sizeof($a);// number of loops for while
$REQ="* ";// Rquired symbol for form display ie: *E-mail --> meaning E-Mail is required field
if (isset ($_POST) ){
$numFormDisplays=1;
}
$z=0;
$errorsArray[$lable]="";
foreach ($_POST as $post => $submission) { //Breakup variables for processing
$eval = $post;
$eval2 = $post;
$findLableMarker = 0;
$eval = preg_replace('/[^0-9]/','', $eval) ;//delete all non numbers for decoding ending numbers
$isRequired=( substr ($eval, 2, 1) );//decide if entry is required
$howToValidate=( substr ($eval, 0, 2) );// decide how to validate entry
$findLableMarker = strrpos($eval2,"@", 0);
$lable=( substr($eval2, 0, $findLableMarker) );// find lable as in form <lable value="E-Mail"
$findLableMarker = strlen($lable);
$findLableMarker = $findLableMarker+1;
$findforMarker = strrpos($eval2,"%");
$findforMarker = $findforMarker - $findLableMarker;
$for=( substr($eval2, $findLableMarker, $findforMarker) );// find for as in form <input for="eMail"
if (isRequired == 1 && empty ($submission) ){// check if field is required
$cssErrors[$lable]=$lable;
$formErrors=$formError+1;
$errorsArray[$lable]="Your $lable is required to process your Request!";
}
if ($submission) {
if ($howToValidate=01) {// validate General ie: name address city state
if (ereg ("<|>|\\\\|,|:|\^|\]|\[|;", $submission ) ){//(ereg ("string", $date))
$cssErrors[$lable]=$error;
$formErrors=$formError+1;
$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
"'s cannot contain the following characters: < > \ , : ^ ] [ ;";
}
}
if ($howToValidate=02){// validate as an e mail
if (ereg("\(|\)|<|>|\\\\|,|:|\^|\]|\[|;", $submission) ) {
$cssErrors[$lable]=$error;
$formErrors=$formError+1;
$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
"'s cannot contain the following characters: < > \ ' , : ^ ] [ ;";
}
if ( !eregi (".*[.]*.+@.*\.{1}.*", $submission) ){
$cssErrors[$lable]=$error;
$formErrors=$formError+1;
$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
"'s must contain an @ and a . for them to be processed properly";
}
}
if ($howToValidate=03){// Validate as a phone number
$phone = $submission;
$phone = preg_replace('/[^0-9]/','',$phone);
if (strlen($phone)==10) {
$areacode = substr($phone, 0,3);
$exch = substr($phone,3,3);
$phoneEnd = substr($phone,6,4);
$submission = "(" . $areacode . ") " . $exch . "-" . $phoneEnd;
}
if (strlen($phone)==11) {
$countryCode = substr($phone, 0, 0);
$areacode = substr($phone, 1, 3);
$exch = substr($phone,4,3);
$phoneEnd = substr($phone,7,4);
$submission = $countryCode. " "."(" . $areacode . ") " . $exch . "-" . $phoneEnd;
}
else {
$cssErrors[$lable]=$error;
$formErrors=$formError+1;
$errorsArray[$lable]="The $lable you entered was invalid!<br/>$lable".
"'s must either 10 or 11 digits ie: 555-555-5555 or 1 (555) 555-5555";
}
}
}
if ( ( /*$formErrors*/1 > 0) || ( empty ($numFormDisplays) ) ){// display form with or without previously entered data
/*if ( empty ($numFormDisplays) ){// empty error arrays if form hasn't been displayed
unset ($cssErrors, $formErrors, $errorsArray);
}*/
if (isRequired == 0 ){
$REQ="";
}
$form=" <tr><td><label for=\"".$for."\">".$REQ.$lable."</label></td><td>"."<input name=\"".
$lable."@".$for."%".$howToValidate."-".$isRequired."\"".
"type=\"text\" id=\"$for\" value=\"".$submission."\" size=\"37\" /></td></tr>";
if ($z < 1){// only print begining of the form once
$beginForm="<form action=\"http://localhost/bkiser.net/public/Test.php\" method=\"post\"><table border=\"0\"".
"cellpadding=\"8\" cellspacing=\"8\" summary=\"feedback form\">";
}
else $beginForm="";
if ($z == ( ($numFormElements)-(1) ) ){// only print end of the form once
$endForm="<tr><td align=\"center\" colspan=\"2\"><input type=\"".
"submit\" value=\"Send Feedback\" /></td></tr></table></form>";
}
else $endForm="";
}
if (empty ($formErrors) ){// mail form
$g=$g;
}
$z++;//used to keep $beginForm (the form tag itself) from printing twice
echo $beginForm . $form . $endForm."<br/>";
if ($z == ( ($numFormElements)-(1) ) ){
echo"<div id=apDiv1>";
//print_r(array_keys($cssErrors));
//echo("<br/>");
}
print_r(array_keys($errorsArray));
print_r($formErrors."<br/>");
if ($z == ( ($numFormElements)-(1) ) ){
echo"</div>";
}
}// if submission
?>
/* Print inputs, with labels */
foreach ($aInputMap as $k => $v)
echo '<label>'.$v['label'].'</label><input type="text" name="'.$k.'" length="'.$v['length'].'" />';
/* Validation */
$bValid=true;
foreach ($aInputMap as $k => $v)
{
/* If this is a required input, and there isn't a corresponding $_POST */
if (($v['required']) && !isset($_POST[$k]))
{
$bValid=false;
break;
}
/* Ensure input contents match regexp */
$aMatch=array();
preg_match($v['regexp'],$_POST[$k],$aMatch);
if ($aMatch[0][0] != $_POST[$k])
{
$bValid=false;
break;
}
}