View Full Version : Submitting a form via a JS function..

Jun 1st, 2003, 07:29 PM
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?

Jun 2nd, 2003, 12:40 AM
You can submit Forms with JavaScript.


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.



Jun 2nd, 2003, 03:28 AM
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.


Jun 2nd, 2003, 04:21 AM
is this what you're looking for?

<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);
case "select-one":
qs += "&" + el.name + "=" + ((el.options[el.selectedIndex].value) ? escape(el.options[el.selectedIndex].value):escape(el.options[el.selectedIndex].text));
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));
case "checkbox":
case "radio":
if (el.checked) qs += "&" + el.name + "=" + escape(el.value);
//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;

<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 name="sel2" size="5" multiple>
<option value="1">Item1</option>
<option value="2">Item2</option>
<option value="3">Item3</option>
<input type="button" value="Submit" onclick="doSubmit()">

Jun 2nd, 2003, 05:47 AM
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.


Jun 2nd, 2003, 06:14 AM
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

Jun 2nd, 2003, 08:39 PM
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.