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
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Angry Submitting a form via a JS function..

    Here's the current dilemma:

    I am trying to submit a form using an external javascript file that actually does the interaction with the server, then have the external javascript file redirect to another page.

    ie - pseudocode/timeline

    1. user fills out form/presses "submit" button

    2. "submit" button calls JS function from a dynamically loaded JS file, (see lilorean's 2nd method in the 2nd post in <a href="http://www.codingforums.com/showthread.php?s=&threadid=17728">this</a> thread.

    3. this JS function passes everything from the form to the server

    4. a line at the end of the function says something like "window.location = page2.html"


    Now, I know that I could do it by having the JS function put everything into the querystring of the external JS file that will be loaded, but I'm hoping I can avoid doing this for each different field and just pass the whole form somehow.

    Any ideas?
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #2
    piz
    piz is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Barcelona
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can submit Forms with JavaScript.

    document.forms[0].submit();

    Where 0 is the index of the form to submit in the document.

    You can use the name of the form instead, if your form does have a name="some_form_name" attribut.

    document.forms['some_form_name'].submit();

    Greetz
    piz

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    true... but I could do that with a submit button and the "action" of a form.

    What I'm looking for is the ability to get the info from the form to the server without the user's browser ever having to display a page on that server.

    I'm making an HTML catalogue that will be distributed on CD. I would like to be able to have the form submitted to our web server without the user ever having to view a page on the server.

    As mentioned above I can do it by having a button in the form that, when clicked, calls a function that builds a querystring containing all of the info from the form, and then loads an external JS file from the server with that querystring. The external JS file would actually be an ASP file that takes the info from the querystring and records it in a database. It then returns a line to the browser that says something like window.location=nextpage.html, so that so far as the browser is concerned it's simply getting a JS redirect command, however the info from the form does end up going to the server.

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    is this what you're looking for?

    Code:
    <html>
    <head>
    <script language="javascript">
    function doSubmit(){
    
    	var f = document.myform;
    	var qs = "";
    	var el;
    	for (var i=0;i<f.elements.length;i++){
    		el = f.elements[i];
    		switch (el.type){
    			case "text":
    			case "textarea":
    				qs += "&" + el.name + "=" + escape(el.value);
    				break;
    			case "select-one":
    				qs += "&" + el.name + "=" + ((el.options[el.selectedIndex].value) ? escape(el.options[el.selectedIndex].value):escape(el.options[el.selectedIndex].text));
    				break;
    			case "select-multiple":
    				for (var j=0;j<el.options.length;j++){
    					if (el.options[j].selected)
    						qs += "&" + el.name + "=" + ((el.options[j].value) ? escape(el.options[j].value):escape(el.options[j].text));
    				}
    				break;
    			case "checkbox":
    			case "radio":
    				if (el.checked) qs += "&" + el.name + "=" + escape(el.value);
    				break;
    		}
    	}
    	//remove the first &
    	if (qs) qs = qs.substring(1);
    
    	//submit to the server "silently"
    	var img = new Image();
    	img.onerror = function(){location.href='nextpage.html'} //this will be invoked since img.src is not an image
    	img.src = "http://yoursite.com/yourpage.asp?" + qs;
    	
    }
    </script>
    </head>
    <body>
    <form name="myform">
    <input name="txt" value="text"><br>
    <textarea name="txta">my text area</textarea><br>
    <input type="checkbox" name="chk" value="1">My checkbox<br>
    <input type="radio" name="rad" value="1">My radio 1<br>
    <input type="radio" name="rad" value="2">My radio 2<br>
    <select name="sel1">
    <option>Select something</option>
    <option value="1">Item1</option>
    <option value="2">Item2</option>
    </select><br>
    <select name="sel2" size="5" multiple>
    <option value="1">Item1</option>
    <option value="2">Item2</option>
    <option value="3">Item3</option>
    </select><br>
    <input type="button" value="Submit" onclick="doSubmit()">
    </form>
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Pretty much,

    I was hoping I wouldn't have to loop through all of the form elements. If you know of a way to avoid it that would be great. Otherwise it looks like I'll have to do some looping.

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    How does your form look like? The code I gave is generic. You can use it in any form with any fields. We can make a specific code for your form fields if you want

  • #7
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Its mostly a bunch of text fields, with a couple of selects.

    I can use the generic code above. I may take out the parts that I won't need - we'll see. I just read it over again when I am less tired and it made a lot more sense to me.

    Thanks!

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com


  •  

    Posting Permissions

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