Do not send via POST elements within div "display None"

Asked

Viewed 689 times

5

I want to remove the Ivs that are left with display:none with the function remove() jquery and when they are selected as display:block they come back.... because I want to send a form via POST that cannot receive the elements inside the Ivs that have display:none.

  • 1

    Just to comment that no solution presented on the client side (javascript or css for example) should be used as a security system that that data will not be sent, as it can easily be reactivated by the console or plugins, then additional checks should be performed on the server side.

2 answers

8

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

Joins in logic to also hide logic to disable.

5


As @Sergio said, you can disable the fields you don’t want to send, follow the javascript code:

$(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 trigger the function you seek inputs inside divs hidden and disabled them.

OBS.: If there is a need to put other fields that are not inputs, as an example textarea, just separate by comma on the dial, 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.