Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid sending input fields which are hidden by display:none to a server?

Tags:

html

http

field

Imagine you have a form where you switch visibility of several fields. And if the field is not displayed you don't want its value to be in request.

How do you handle this situation?

like image 723
glaz666 Avatar asked Sep 03 '09 15:09

glaz666


People also ask

How do you make an input field invisible?

The <input type="hidden"> defines a hidden input field.

Are display none inputs submitted?

display:none - doesn't mean that form elements are not submitted (just not displayed)... They are submitted in the current version of Chrome, despite being hidden.

How do I hide input fields in inspect element?

It is not possible to hide elements from the DOM inspector, that would defeat the purpose of having that tool. Disabling javascript is all it would take to bypass right click protection. What you should do is implement a proper autologin.


2 Answers

Setting a form element to disabled will stop it going to the server, e.g.:

<input disabled="disabled" type="text" name="test"/> 

In javascript it would mean something like this:

var inputs = document.getElementsByTagName('input'); for(var i = 0;i < inputs.length; i++) {     if(inputs[i].style.display == 'none') {         inputs[i].disabled = true;     } } document.forms[0].submit(); 

In jQuery:

   $('form > input:hidden').attr("disabled",true);    $('form').submit(); 
like image 135
karim79 Avatar answered Oct 11 '22 22:10

karim79


You could use javascript to set the disabled attribute. The 'submit' button click event is probably the best place to do this.

However, I would advise against doing this at all. If possible you should filter your query on the server. This will be more reliable.

like image 21
Benedict Cohen Avatar answered Oct 11 '22 22:10

Benedict Cohen