Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new hidden input fields to the form on submit

Tags:

javascript

I want to pass certain hidden parameters in a request when some conditions are met.

For example I want to pass these if this condition is true:

<script type="text/javascript"> 
 function checkClosureLevel()
 {
    var openLevel = document.getElementById('openLevel');
    var phyCompLevel = document.getElementById('phyCompLevel');
    var finCompLevel = document.getElementById('finCompLevel');

    if (openLevel.checked) { 
      //PASS HIDDEN FORM VARIABLES HERE AND SUBMIT FORM 
    }
 }
</script>

<form action="process.det_details" method="post" name="detParameterForm">
  <fieldset class="det">
    <legend>Closure Level</legend>
    <input type="checkbox" name="openLevel" >Open</input><br/>
    <input type="checkbox" name="phyCompLevel" >Physically Complete</input>
    <br/>
    <input type="checkbox" name="finCompLevel" >Financially Complete</input>
  </fieldset>
</form>
like image 682
Doc Holiday Avatar asked Feb 12 '12 18:02

Doc Holiday


People also ask

How do you make an input field hidden?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

Do hidden form fields get submitted?

Code Example The hidden value of type defines a form field that is never displayed to the user. The user cannot change the value of the field, or interact with it. When the user submits the form, all of the data they have entered is sent, including the data stored invisibly in the hidden fields.

How do I hide hidden 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.

How do you pass an input hidden array?

If you want to post an array you must use another notation: foreach ($postvalue as $value){ <input type="hidden" name="result[]" value="$value."> } Save this answer. Show activity on this post.


1 Answers

The document.forms object is a collection of all <form> elements in the page. It has numeric indexes, and named items. The named items correspond to a name attribute of each <form>.

var theForm = document.forms['detParameterForm'];

To make appending data easier, you can create a function which adds data for a given form.

function addHidden(theForm, key, value) {
    // Create a hidden input element, and append it to the form:
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = key; // 'the key/name of the attribute/field that is sent to the server
    input.value = value;
    theForm.appendChild(input);
}

// Form reference:
var theForm = document.forms['detParameterForm'];

// Add data:
addHidden(theForm, 'key-one', 'value');
addHidden(theForm, 'another', 'meow');
addHidden(theForm, 'foobarz', 'baws');

// Submit the form:
theForm.submit();
like image 188
Rob W Avatar answered Sep 17 '22 16:09

Rob W