www.webdeveloper.com
Results 1 to 3 of 3

Thread: Modify hidden fields after onsubmit

  1. #1
    Join Date
    May 2014
    Posts
    1

    Unhappy Modify hidden fields after onsubmit

    Hi,

    I am struggeling with onsubmit and changing the values of hidden fields with JS before the form is sent.
    some Code:

    Sample HTML:
    <form method="_GET" action="gps.php" id="form1" onsubmit="return submitForm1();">
    <input type="hidden" name="foo" id="foo" value="0">
    <div onclick="$('#form1').submit();">Submit the form</div>
    </form>

    JS:

    function submitForm1() {
    $('#foo').val('foo new value');
    alert($('#foo').val());
    return true;
    }

    When I submit the form a message box appears and tells me "foo new values". On my server the corresponding GET-Value which is sent is still "0"

    This ist just an example - in real world I am doing GEOCODING of an address or using geo.location for reading gps coords.

    Any help is appreciated.
    Sven


  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    May 2014
    Posts
    912
    Another excellent example of everything wrong with jQuery... and with not building a form PROPERLY. There isn't even such a thing as _Get as a method. (what is it with these fictional made up METHOD's everyone's using lately?)

    First let's fix the form:
    Code:
    <form method="get" action="gps.php" id="form1">
    	<fieldset>
    		<input type="hidden" name="foo" id="foo" value="0" />
    		<input type="submit" value="Submit the form" />
    	</fieldset>
    </form>
    Then intercept the submit properly:

    Code:
    (function(d) {
    	var
    		foo = d.getElementById('foo'),
    		form = d.getElementById('form1');
    	
    	function eventAdd(e, event, handler) {
    		if (e.addEventListener) e.addEventListener(event, handler, false);
    			else this.attachEvent('on' + event, handler);
    	}
    	
    	if (form) eventAdd(form, 'submit', function(e) {
    		foo.value = 'foo new value';
    		alert(foo.value);
    	});
    	
    })(document);
    Anonymous function so there's little risk of conflicting with other code, uses the proper getElementById methods, attaches the event from the scripting instead of the markup attaching to the script, gets rid of that bizarre value wrapping jQuery has for no good reason -- and as such, look ma, No jQuery.

    Cleaner, simpler, faster, more effective, more likely to leverage caching -- without the library nonsense.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles