We help IT Professionals succeed at work.

PHP Function based on css class="required"

DennisHacker
DennisHacker asked
on
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:
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

Open in new window

Comment
Watch Question

I'd strongly suggest doing all validations using JavaScript.
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,...
	// get form data
	foreach ($form_elements as $element)
	{
		$form[$element] = htmlspecialchars($_POST[$element]);
		if ($form[$element] = '')
		{
			$valid_form = FALSE;
		}
	}

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.

Author

Commented:
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.
Using a JavaScript Framework to post data from the form to server automatically ignores hidden (as display=none, not type=hidden) fields. Even using standard JavaScript it is possible to ignore all hidden form data. Thus your PHP would end up being very simple as expressed in my previous post.

The way you have the PHP reading Form data, you would only be receiving name and value pairs.
ie: name: 'organizerPhone', value: '0123456789'

Post data does not include the class applied to the Form element with a submit. Your PHP code has no way to differentiate between a hidden submitted empty field, or a class="required" blank field.

A way to possibly make the PHP control everything would be to add a prefix to every non-hidden field on a submit. Means both the browser and server do more work, but you get to have all calculations in the PHP.
Example using prefix of "REQ_":
	// get form data
	foreach ($form_elements as $element)
	{
		if (strpos($_POST[$element], 'REQ_') 
		{
			$form[$element] = htmlspecialchars($_POST[$element]);
			if ($form[$element] = 'REQ_') // just REQ_ is the same as a blank, as every required field has this added.
			{
				$valid_form = FALSE;
			}
		}
	}

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.