Do not send via POST elements inside div "display none"

5

I want to remove the divs that are with display:none with the remove() function of jquery and when they are selected as display:block they come back .... since I want to send a form via POST that can not receive the elements inside of the divs that have display:none .

    
asked by anonymous 18.11.2016 / 05:19

2 answers

5

As @Sergio said, you can disable the fields you do not want to send, follow the code in javascript:

$(document).ready(function() {
    $('form').submit(function() {
        $(this).find('div:hidden input').prop('disabled', true);
    });
});

I monitor the submission of the form, when this event is triggered I fire the function that looks for inputs within divs hidden and disable them.

NOTE: If you need to put other fields that are not inputs , such as textarea , simply comma-separated the selector like this:

.find('div:hidden input,textarea')

The structure of HTML would look like this:

<form>

  <div style="display: none">
    <input type="text" name="campo1">  
  </div>

  <div>
    <input type="text" name="campo2">  
  </div>

</form>
    
18.11.2016 / 11:38
8

To avoid that input elements are sent with a form , the common solution is to do input.disabled = true; , that is, with input it is not sent to the server.

Join logic to hide logic also to disable.

    
18.11.2016 / 07:23