DennisHacker
asked on
PHP Function based on css class="required"
I have a dynamic form that shows and hides table rows containing inputs on a form with javascript. It also changes the class attribute for each of the input elements in a visible row to "required".
On smaller forms, I set up an array called $formElements that lists every element on the form. If the form is submitted, I run the htmlspecialchars function on the element, and then have individual if statements that check if the $_POST variable for the element is blank, generate an error, and set a $valid_form variable to FALSE, preventing the submission of the form.
As you can imagine, as the form grows in numbers of variables, that list is repetitive, and very long. Additionally, it's not practical, because if a row is not visible, I don't want the elements in that row to be required.
I would like to write a php function that would:
Is this a place where a function is best used? If so, can one function be used for both purposes?
On smaller forms, I set up an array called $formElements that lists every element on the form. If the form is submitted, I run the htmlspecialchars function on the element, and then have individual if statements that check if the $_POST variable for the element is blank, generate an error, and set a $valid_form variable to FALSE, preventing the submission of the form.
As you can imagine, as the form grows in numbers of variables, that list is repetitive, and very long. Additionally, it's not practical, because if a row is not visible, I don't want the elements in that row to be required.
I would like to write a php function that would:
apply the htmlspecialchars function to the $_POST of each variable
if class="required" and the $_POST of the variable = "", then set the $error['element'] = $error_open . "Please fill in required field." . $error_close; and set the $valid_form variable to FALSE.
Is this a place where a function is best used? If so, can one function be used for both purposes?
HERE IS A PORTION OF THE CODE THAT I'M USING SO FAR...
if (isset($_POST['submit']))
{
// process form
// get form data
foreach ($form_elements as $element)
{
$form[$element] = htmlspecialchars($_POST[$element]);
}
// check form elements
// check required fields
if ($form['organizerName'] == '')
{
$error['organizerName'] = $error_open . "Please fill in all required fields!" . $error_close;
$valid_form = FALSE;
}
if ($form['organizerPhone'] == '')
{
$error['organizerPhone'] = $error_open . "Please fill in all required fields!" . $error_close;
$valid_form = FALSE;
}
ETC., ETC. FOR EACH OF THE FORM ELEMENTS
ASKER
What you've done above would work if all of the elements were required. However, I have not set ALL of the visible elements to class="required." I really need something that will set the $valid_form = FALSE if an element with class="required" is not filled in, and still allow the form to process if elements that are not required are still blank.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'd also suggest making life a little simpler as well, by using a framework such a jQuery so you have access to .serialize(), .submit(), .ajax() etc...
This way your PHP and all server side code can be focused on working with valid data.
To answer your direct question,...
Open in new window
Then you do not need to state every single individual form element listed as you've shown.
As you are making all visible fields "required", then only visible fields should be passed to server. Thus back to my suggestion of using JavaScript to ensure this occurs, you would be better off to also check for empty fields where class="required" in the JavaScript as well.