Hello everyone
I've written a function for form validation but I can't find a way to make it work with fields that are arrays.
I know it's pretty lame to ask for help on a quite big function that I've written on my own with my own weird logic but you guys are the only ones who could help me or at least, give ideas.
Many thanks to those who will help
Ok, here is my function that I would like to enhance :
Here is how the function is called :
I've written a function for form validation but I can't find a way to make it work with fields that are arrays.
I know it's pretty lame to ask for help on a quite big function that I've written on my own with my own weird logic but you guys are the only ones who could help me or at least, give ideas.
Many thanks to those who will help
Code:
// my function works with such field names
<input type="text" name="form_val" value="1" /> value 1
// my function doesn't work with such field names
<input type="checkbox" name="form_val[]" value="1" /> value 1
<input type="checkbox" name="form_val[]" value="2" /> value 2
<input type="checkbox" name="form_val[]" value="3" /> value 3
or
<input type="checkbox" name="form_val[11][]" value="1" /> value 1
<input type="checkbox" name="form_val[11][]" value="2" /> value 2
<input type="checkbox" name="form_val[11][]" value="3" /> value 3
or
<input type="checkbox" name="form_val[22][]" value="1" /> value 1
<input type="checkbox" name="form_val[22][]" value="2" /> value 2
<input type="checkbox" name="form_val[22][]" value="3" /> value 3
Ok, here is my function that I would like to enhance :
Code:
// SHOW ERRORS
// ---------------------------------------------------------------------------
function show_errors($one_form, $one_title, $one_name, $one_message) {
static $field_names = "";
global $set_forms_config;
if($_POST OR $_FILES) $postarray = array_merge($_POST, $_FILES);
// display form
if($one_title != NULL AND $one_name != NULL) {
$GLOBALS['curfieldname'] = $one_name;
static $title_num = 0;
// format of default title
$title_bef = "<span class=\"form-title-error\">";
$title_aft = "</span>";
// if values submitted
if($_POST["form_name"] == $one_form) {
if($postarray[$one_name] == "" OR strstr($field_names, $one_name)) {
static $error_num = 1;
// format title when error
$title_bef = "<a name=\"" . $one_name . "\"><span class=\"form-title-error\">";
$title_aft = "</span></a>";
if ($field_names == "") $title_bef_num = "<b>[ " . $error_num . " ]</b> ";
$one_title = $title_bef_num . $one_title;
$error_num++;
} else {
// format title when ok
$title_bef = "<span class=\"form-title-ok\">";
$title_aft = "</span>";
}
// on very first sight
} else {
$_SESSION["hd_forms"][$one_form]["field_title"][] = $one_title;
$_SESSION["hd_forms"][$one_form]["field_name"][] = $one_name;
}
return $title_bef . $one_title . $title_aft;
// show empty fields
} else if($one_title == NULL AND $one_name == NULL) {
for($i=0; $i < sizeof($_SESSION["hd_forms"][$one_form]["field_name"]); $i++ ) {
$temp_name = $_SESSION["hd_forms"][$one_form]["field_name"][$i];
// - ! - if field for array
if (strstr($temp_name, "]")) {
// - ! - if field not for array
} else {
$temp_val = $postarray[$temp_val];
}
if($temp_val == "") {
$GLOBALS['errors'] .= "
<b>
" . str_replace("}_{", "
<a href=\"javascript:
document.location.href='#" . $_SESSION["hd_forms"][$one_form]["field_name"][$i] . "';
document." . $postarray["form_name"] . "." . $_SESSION["hd_forms"][$one_form]["field_name"][$i] . ".className += ' form-field-error';
document." . $postarray["form_name"] . "." . $_SESSION["hd_forms"][$one_form]["field_name"][$i] . ".focus();\"
onMouseOver=\"status='" . addslashes($_SESSION["hd_forms"][$one_form]["field_title"][$i]) . "';return true\"
class=\"form-title-error\">
[ " . ($error_num + 1) . " ]
" . $_SESSION["hd_forms"][$one_form]["field_title"][$i] . "
</a>
", hd_page_textfill(1)) . "
</b><br>\n
";
$error_num++;
} else {
}
}
} else {
// specific detection
if(!$error_num) static $error_num = 1;
$temp_array = explode(" . ", $one_name);
$one_name = $temp_array[0];
for($i=0; $i < sizeof($temp_array); $i++ ) {
$field_names .= $temp_array[$i] . " . ";
$js_link .= "
document." . $postarray["form_name"] . "." . $temp_array[$i] . ".className += ' form-field-error';
document." . $postarray["form_name"] . "." . $temp_array[$i] . ".focus();
";
}
$GLOBALS['errors'] .= "
<a href=\"javascript: " . $js_link . "\"
onMouseOver=\"status='';return true\"
class=\"form-title-error\">
[ " . $error_num . " ]
" . $one_message . "
</a>
<br>\n
";
$error_num++;
return $GLOBALS['errors'];
}
}
// ---------------------------------------------------------------------------
// RESET
// ---------------------------------------------------------------------------
if(!$_POST) {
unset($_SESSION["hd_forms"]);
}
Here is how the function is called :
Code:
// Call upon form display
...
<tr>
<td>" . show_errors($form_name, "Product Reference", "form_prod_ref", NULL) . "<br />
<input type=\"text\" name=\"" . $curfieldname . "\" value=\"" . $_POST[$curfieldname] . "\" class=\"field1\" maxlength=\"33\" /></td>
</tr>
<tr>
<td>" . show_errors($form_name, "Product Name", "form_prod_name", NULL) . "<br />
<input type=\"text\" name=\"" . $curfieldname . "\" value=\"" . $_POST[$curfieldname] . "\" class=\"field1\" maxlength=\"33\" /></td>
</tr>
...
Code:
// Call upon form validation
// - ! - find empty fields
show_errors($_POST["form_name"], NULL, NULL, NULL);
// - ! - if no empty fields => start specific detection
if (!$errors) {
if(strlen($_POST[""form_prod_ref""]) > 10) show_errors($_POST["form_name"], NULL, ""form_prod_ref"", "!This field must not contain more than 10 characters!");
}
if (!$errors) {
// - ! - continue script....
}