0

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

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

With the code above the input will be posted when the form is submitted. But How can I avoid submitting the input element whose parent is hidden? Any idea will be appreciated.

Community
  • 1
  • 1
macio.Jun
  • 9,647
  • 1
  • 45
  • 41
  • If you are not passing, then why ur creating..if its on selection basis try to manage it via if else – swapnesh Dec 31 '12 at 03:56
  • I have a div element, which contains 20 input elements. I have js code, which hides the div element. – macio.Jun Dec 31 '12 at 04:03
  • You may also refer this post : http://stackoverflow.com/questions/3008035/stop-an-input-field-in-a-form-from-being-submitted – 000 Dec 31 '12 at 04:05
  • Thank you so much for you two links Rishi. I think probably there is no direct solution. – macio.Jun Dec 31 '12 at 04:44
  • For those who want achieve this effect, plz refer to: http://jsfiddle.net/gKsTS/ – macio.Jun Jan 25 '13 at 04:48

5 Answers5

1

While posting your form, have a onClick function and write below code in it. This code will help you to find all the div tags that is display:none and is the div is display:none, it will disable all the input elements.

Check this http://jsfiddle.net/xyuY6/3/

var divs= document.getElementsByTagName('div');
for(var i = 0;i < divs.length; i++) {
    if(divs[i].style.display == 'none') {
        if ( divs[i].hasChildNodes() ){
           var inputs = divs[i].getElementsByTagName('input');
           for(var j = 0;j < inputs.length; j++) {
               inputs[j].disabled = true;
           }
        }
    }
}
Sahal
  • 4,046
  • 15
  • 42
  • 68
0

Using display:none instead will fix your problem.

scottlimmer
  • 2,230
  • 1
  • 22
  • 29
  • Sry man, it should be none. But even if the div is hidden, its child element input will still be passed when clicking submit button. – macio.Jun Dec 31 '12 at 03:59
  • What about setting display:none on the input rather than the div? – scottlimmer Dec 31 '12 at 04:02
  • Because I have 20 input elements in the div element, and I have js code, which hides the div element. add display:none to each input element is not what I want. – macio.Jun Dec 31 '12 at 04:05
0

Set disabled on the input element.

Steven Mercatante
  • 24,757
  • 9
  • 65
  • 109
  • Because I have 20 input elements in the div element, and I have js code, which hides the div element. add display:none to each input element is not what I want. – macio.Jun Dec 31 '12 at 04:07
0

This is not a PHP issue, it is an HTML/DOM issue. As one person commented on your original thread, if you are generating this code via PHP, you should include disabled='disabled' on your input element as well to prevent submit and remove the disabled attribute when the parent element is unhidden via javascript to allow submission of element value again.

I would add HTML and Javascript tags to this answer if you want further help.

000
  • 3,976
  • 4
  • 25
  • 39
Joey T
  • 774
  • 7
  • 11
  • But If I have 2 divs, each of which has 20 inputs. And I have another select element containing two options: o1, o2. If I select the o1, then the second div is set hidden. If I select o2, then the first div is set hidden. I don't wanna set both div and all 20 inputs hidden. Ty for your reply. – macio.Jun Dec 31 '12 at 04:21
0

If there is no other way what you can do is before submit, remove all child elements from the hidden divs using Javascript so that you don't get them in POST.

deej
  • 2,536
  • 4
  • 29
  • 51