Tutorial :How to process ‘only’ visible fields in an HTML Form?



Question:

Is there a way not to process fields with a “display:none” style ? The fields are dynamically hidden using javascript but are processed normally in PHP.

I’m looking for a PHP solution (no javascript).


Solution:1

Depending on how your form is structured, it might be a viable method to remove the name attribute of the hidden inputs. in whatever Javascript you use to hide the input, add this line:

myInputElement.removeAttribute('name');  

Inputs without a name are not submitted with the form. If you might need to revert back (and show the inputs again), you might want to think about storing the name somewhere retrievable: perhaps use the same name as the id on each element, or store it into the element's class attribute somewhere.


A better idea, as suggested by Colin, would be to disable the element instead. This will have the same effect as removing its name, however it would be much easier to revert, should you need to.

myInputElement.disabled = true;  


Solution:2

My recollection is that input's that are disabled will not be submitted in the form data. So when you hide the field set the disabled attribute as well. No need to muck with names and ids.

From the W3C:

In this example, the INPUT element is disabled. Therefore, it cannot receive user input nor will its value be submitted with the form.

<INPUT disabled name="fred" value="stone">


Solution:3

You could use javascript to set all the values in hidden fields to ''. Optionally a library like jQuery could help you select the fields based on your style (style="display:none;") in an easier way...


Solution:4

Remove the name attribute on the various fields. PHP won't process them.


Solution:5

I'm not sure you can do this without making at least some changes to the Javascript.

Since the fields are hidden in the browser (probably using CSS), there's no way for the PHP script that receives the form to know which ones are hidden (it doesn't get the current CSS state) unless the javascript helps out.

I'd recommend, as suggested before, getting the javascript to remove the name attribute so the fields aren't submitted.


Solution:6

There is no way for PHP to detect if a HTML form was hidden. The only ways are: A) Unsetting the name with JS

The problem with this is if you want to re enable the form element you lose the name and have to have somem way of storing it to replace it.

B) Adding an additional hidden field stating that the field is hidden (with JS again) This will add more data to the HTTP POST, which could be considered a disadvantage. Plus you'd then need to have PHP in place to check if the "fieldHidden" $_POST var is set.

An example of this would be to create a hidden element with a name of "HiddenFields[hiddenFieldName]" and then using a if array_key_exists with the name of the post variable you are checking.

This is sort of convulated, but will still let PHP have acccess to the variable if you so desire.

C) setting the field to read only. (again, with JS) This would work, I think. But then PHP doesn't have access to it, which may be an issue.


Solution:7

You could do this (using jQuery as an example) by using the :visible selector to see which form elements are visible, and then either serializing the visible ones into a form and submitting it, or removing the invisible ones.

http://docs.jquery.com/Selectors/visible http://docs.jquery.com/Selectors

http://docs.jquery.com/Ajax/serialize http://docs.jquery.com/Ajax

However, it would be MUCH easier for you to just make it so that form elements are made empty when they become invisible, and then make them optional on the server side.

EDIT There's no way I can think of to have PHP not process them unless you send it instructions one way or another. Anything that has a name will be processed, except checkboxes, which are indicated off by lack of a value. As far as I know, there is no way for PHP to tell what the visible status of a field was on the client side when the form was submitted.

Perhaps you could add the names of disabled/invisible fields to an extra hidden field, concatenated by commas or something, and build the PHP to not process fields that appear in it. Otherwise you could manipulate the field names to start with proc_ and noproc_ or something, and use that as an instruction whether to process the field, but again you are just passing extra instructions to PHP.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »