Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Souteast Coast
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    stop sending empty hidden values

    I have quite a few hidden form objects that are almost always empty values. Unless a user clicks on something. Then javascript places a value within those hidden form objects.

    So when the user clicks send, I want only the hidden objects with values to be sent in the substring.

    I accomplished this by using a for loop, going through every form object (document.forms[1].elements[g]), and appending the name and value of those objects to a variable. then send the variable's informtion through window.location.href

    is there another way to do this without using as much code as i did?

    here is what i have:

    Code:
    function validateForm() {
    var mainForm = document.search;
    
    // if the "(Optional)" is not cleared it will be sent and produce an error
    	if(mainForm.name.value == "(Optional)") {
    			mainForm.name.value = "";
    		}
    	var results = new String();
    	var mainForm = document.forms[1];
    //runs through all form elements and appends only the elements with values to a link to be sent
    	for(i=0;i<document.forms[1].length;i++) {
    		//test if textbox
    		if(mainForm.elements[i].type == "text" && mainForm.elements[i].value != "") {
    				results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
    		}
    		//test if hidden box
    		if(mainForm.elements[i].type == "hidden" && mainForm.elements[i].value != "") {
    				results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
    		}
    		//test if raido button
    		if(mainForm.elements[i].type == "radio" && mainForm.elements[i].value != "hd" && mainForm.elements[i].checked == true) {
    				results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
    		}
    		//test if select box that might have values
    		if(mainForm.elements[i].name == "state" || mainForm.elements[i].name == "country" || mainForm.elements[i].name == "brand") {
    			if(mainForm.elements[i].selectedIndex > 0) {
    				results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
    			}
    		}
    		//test for the rest of the select boxes that always has values
    		if(mainForm.elements[i].name == "num_adults" || mainForm.elements[i].name == "num_beds" ||
    		mainForm.elements[i].name == "num_rooms" || mainForm.elements[i].name == "doa_mm" ||
    		mainForm.elements[i].name == "doa_dd" || mainForm.elements[i].name == "dod_mm" ||
    		mainForm.elements[i].name == "dod_dd") {
    			results += "&" + mainForm.elements[i].name + "=" + mainForm.elements[i].value;
    		}
    	}	
    	window.location.href="http://www.website.com/power_results.cgi?" + results;
    }

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There's no way around this I'm afraid. If the form fields are in the HTML then they will be submitted. Your work-around is a valid way of overcoming this.

    Perhaps you should consider thinking of a better way of achieving your goal which doesn't use hidden fields.
    As easy as 3.1415926535897932384626433832795028841

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can disable each hidden element at submission. Quckie example

    Code:
    function disableEmpty( f )
    {
    	for ( var i = 0; ( elem = f.elements[i] ); i++ )
    	{
    		if ( elem.type == 'hidden' and elem.value = "" )
    			elem.disabled = true;
    	}
    }
    Where f is a reference to the form
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is there another way to do this without using as much code as i did?
    YES, there is.

    set the "disabled" attribute to TRUE. The field will not get sent when form is SUBMITted.

    Code:
    function setMarchingOrders (thefield) {
      
       if (thefield.value == "") {
          thefield.disabled = true;
       }else{
          thefield.disabled = false;
       }
    } // setMarchingOrders()
    There's no way around this I'm afraid. If the form fields are in the HTML then they will be submitted. Your work-around is a valid way of overcoming this.
    *Checkboxes that are not checked are not sent
    *<select> <option>s that are not SELECTED are not sent
    *** a select must be <select multiple> to send more than one, even when you explicitly try to script SELECTED.
    * radio buttons that are not SELECTed are not sent
    * text fields that are DISABLED are not sent
    * hidden fields that are DISABLED are not sent.

    You can script these attributes to send or not at your discresion; independent of user entry or user selection.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Souteast Coast
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great I'll give this disable thing a try

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Souteast Coast
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i haven't tried it yet but W3C websites states the following:

    disabled -- Disables the input element when it first loads so that the user can not write text in it, or select it.
    Note: Cannot be used with type="hidden"

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Note: Cannot be used with type="hidden"
    All I can say is I use it sucessfully with "hidden" fields, running on IE 5.5 for Windoze.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •