/    Sign up×
Community /Pin to ProfileBookmark

Javascript form submission

I have been looking all over the web for a way to use javascript to submit my form. I have seen ajax solutions, but they are confusing, and I cannot get any of them to work. Is there a way that I can easily submit my form using javascript or ajax, using POST?

I cannot find a simple solution…

to post a comment
JavaScript

17 Comments(s)

Copy linkTweet thisAlerts:
@mrhooNov 08.2007 — have you tried document.forms[0].submit()?
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 08.2007 — Yes, I have tried that.

Let me rephrase my my question. I am wanting to use javascript, or AJAX, because I want to submit the form without redirecting to another page. I have multiple forms on a page, and I need to be quickly filled out, and submitted one after another, while keeping the user on the same page. This is why I am wanting to use javascript/ajax to submit the form while keeping the user on the same page.
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 08.2007 — Yes, I have tried that.

Let me rephrase my my question. I am wanting to use javascript, or AJAX, because I want to submit the form without redirecting to another page. I have multiple forms on a page, and I need to be quickly filled out, and submitted one after another, while keeping the user on the same page. This is why I am wanting to use javascript/ajax to submit the form while keeping the user on the same page.[/QUOTE]


I have tried using the following script, but with no luck.

[code=html]<html>
<head>
<title>Simple Ajax Example</title>
<script language="Javascript">

// You don't need to change anything in this function:

function xmlhttpPost(strURL) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring());
}

// The following function obtains two variables from your form (email and message)
// and builds a string that gets sent to your PHP script. Change this function to
// obtain whatever fields you want from your form.

function getquerystring() {
var form = document.forms['form1'];
var email = form.email.value;
var message = form.message.value;
qstr = 'email=' + escape(email) + '&message=' + escape(message);
return qstr;
}

function updatepage(str){
document.getElementById("result").innerHTML = str;
}
</script>
</head>

<form name="form1">
<p>Your Email Address: <input name="email" type="text"><p>
Your Message:<br>
<textarea name="message" rows=10 cols=80></textarea>
<input value="Go" type="button" onclick='javascript:xmlhttpPost("ajax-send-email.php")'></p>
<div id="result"></div>

</form>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@slaughtersNov 08.2007 — Put each form in it's own iFrame and submit them from the main/top document via JavaScript.

window.frames['FRAMENAME'].document.forms[0].submit();
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 08.2007 — I first need to figure out why my ajax form submit script is not working. It is not posting any of the data, and I cannot figure out why!!
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 08.2007 — Ok, got the script working.

But, if I put them in iframes, do I have to put each form on another page? My understanding of an iframe is that you have to have a src page. I don't want to have to put each form on a different page, and pull it in through an iframe.
Copy linkTweet thisAlerts:
@savvykmsNov 08.2007 — This is a bit silly guys, really. Make a hidden <iframe> or have the page within a <frameset> and hide the hidden frame. WITHOUT USING AJAX NOR JAVASCRIPT:
[CODE]
<iframe name="OTHERFRAME" style="display: none;"></iframe>
<form action="somepage.php" method="get" target="OTHERFRAME" name="myform">
User Name: <input type="textbox" name="uname">
E-Mail Address: <input type="textbox" name="uemail">
<input type="submit" value="Submit">
</form>
[/CODE]
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 08.2007 — This is a bit silly guys, really. Make a hidden <iframe> or have the page within a <frameset> and hide the hidden frame. WITHOUT USING AJAX NOR JAVASCRIPT:
[CODE]
<iframe name="OTHERFRAME" style="display: none;"></iframe>
<form action="somepage.php" method="get" target="OTHERFRAME" name="myform">
User Name: <input type="textbox" name="uname">
E-Mail Address: <input type="textbox" name="uemail">
<input type="submit" value="Submit">
</form>
[/CODE]
[/QUOTE]


Wow, now I feel like an idiot. That was such a simple solution and it solved my problem! I guess I was not aware of how to use iframes like that!

Thank you!
Copy linkTweet thisAlerts:
@savvykmsNov 08.2007 — No problem. By the way, this is how I made a chat room script (javascript + php only). I did not know how to use AJAX and still dont know that well, as far as I can tell, all Ajax is, is just another ****ty microsoft created standard. But hey, thats only my opinion. I want to laugh so much at these forums sometimes hahahaha.
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 08.2007 — One more question. Do you know why some browsers would open a new window with the target url it it? Rather than just submitting it into the iframe? I experienced this with Safari so far.
Copy linkTweet thisAlerts:
@savvykmsNov 08.2007 — The original use the the [I]target[/I] HTMl attribute was for frames or popup windows. For examble:

<a href="http://www.google.com/" target="_blank">go to google in another window</a>

<a href="http://www.google.com/" target="A">go to google in another frame</a>

Where "A" is the value of the [I]name[/I] attribute of a parent document's frame in a frameset.

<frameset>

<frame name="A"></frame>

</frameset>

or something, been a while since I did anything with frames.
Copy linkTweet thisAlerts:
@slaughtersNov 08.2007 — The TARGET= approach works, but I'm pretty sure that each form submission will have to wait for the previous to complete. Not an issue if you just have a couple forms on the page. Could be a real pain if you have more than 5 or 6 though.

The multiple iframe approach allows you to use javaScript to execute them all at the same time as each acts asynchronously from the other.

A common technique I use is to throw a DIV around the iframe and then hide/show it, position it where ever I want on the screen, etc.. Makes a nice server side dynamic menu or report element (pseudo-ajax functionality). Anytime I want "reload it" with new values I don't submit to it with a form, I simply re-assign the SRC value of the iframe.
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 09.2007 — The TARGET= approach works, but I'm pretty sure that each form submission will have to wait for the previous to complete. Not an issue if you just have a couple forms on the page. Could be a real pain if you have more than 5 or 6 though.

The multiple iframe approach allows you to use javaScript to execute them all at the same time as each acts asynchronously from the other.

A common technique I use is to throw a DIV around the iframe and then hide/show it, position it where ever I want on the screen, etc.. Makes a nice server side dynamic menu or report element (pseudo-ajax functionality). Anytime I want "reload it" with new values I don't submit to it with a form, I simply re-assign the SRC value of the iframe.[/QUOTE]


I am interested in your approach. I am using multiple iframes. One for each form, and submitting the form into the iframe. Is this the same as you are suggesting? And how can I create a submit button that submits all of my forms?

I see you mentioned to use:
[CODE]window.frames['FRAMENAME'].document.forms[0].submit();[/CODE]

Would I put all of my frame names where FRAMENAME is? Since I have quite a few of them, and my page is dynamically generated, so I never know how many forms there will be (which are used for editing records) nor do I know that frame names before hand since they are generated from each assets ID.
Copy linkTweet thisAlerts:
@slaughtersNov 09.2007 — ....I am using multiple iframes. One for each form, and submitting the form into the iframe. Is this the same as you are suggesting?[/QUOTE]Not really - you are not placing your forms in an iFrame. You are using the iframes as a place to dump your server side responses and keep them invisible to the user. The approach I mentioned works when you want to show output from the server side, but not have the rest of the page flicker, and/or not actually show the results until you want to. Like I said - sort of a pseudo-Ajax approach.
...Would I put all of my frame names where FRAMENAME is....I never know how many forms there will be (which are used for editing records) nor do I know that frame names before hand since they are generated from each assets ID.[/QUOTE]If they do not have a name, then how are you using them as the TARGET= in your submit statements ?

I think your approach is a little strange, but I don't know the project, so if you want to continue using your current approach then dynamically create DIV tags and give each a name based on the "assets ID" and make sure your form submits to it as a TARGET
[code=html]<%
// JSP syntax - Replace with what ever server side code you are using
String sAssetID = "10202";
String sFrameName = "Asset_iFrame" + sAssetID;
%>
<iframe name="<%=sFrameName%>" style="display: none;"></iframe>
<form action="somepage.php" method="get" target="<%=sFrameName%>" name="myform">
User Name: <input type="textbox" name="uname">
E-Mail Address: <input type="textbox" name="uemail">
<input type="submit" value="Submit">
</form>[/code]

You can now just use JavaScript to loop through all the forms in your document and submit them all.
[code]for (i=0; i<document.forms.length; i++) {
document.forms[i].submit();
}[/CODE]
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 09.2007 — Not really - you are not placing your forms in an iFrame. You are using the iframes as a place to dump your server side responses and keep them invisible to the user. The approach I mentioned works when you want to show output from the server side, but not have the rest of the page flicker, and/or not actually show the results until you want to. Like I said - sort of a pseudo-Ajax approach.

If they do not have a name, then how are you using them as the TARGET= in your submit statements ?

I think your approach is a little strange, but I don't know the project, so if you want to continue using your current approach then dynamically create DIV tags and give each a name based on the "assets ID" and make sure your form submits to it as a TARGET
[code=html]<%
// JSP syntax - Replace with what ever server side code you are using
String sAssetID = "10202";
String sFrameName = "Asset_iFrame" + sAssetID;
%>
<iframe name="<%=sFrameName%>" style="display: none;"></iframe>
<form action="somepage.php" method="get" target="<%=sFrameName%>" name="myform">
User Name: <input type="textbox" name="uname">
E-Mail Address: <input type="textbox" name="uemail">
<input type="submit" value="Submit">
</form>[/code]

You can now just use JavaScript to loop through all the forms in your document and submit them all.
[code]for (i=0; i<document.forms.length; i++) {
document.forms[i].submit();
}[/CODE]
[/QUOTE]


First off, how would I make this into a button:

[CODE]for (i=0; i<document.forms.length; i++) {
document.forms[i].submit();
}[/CODE]


My methods for this might seems a little strange, but I am building a way to interact with our CMS that we use for our website. So, I am not coding this from scratch, I have to build a way to interact with the editing interface of the CMS. So, I am using the concept of an excel spreadsheet to have 1 asset per line, which has all the fields available to edit. So the user can list all their assets, update what ever fields they need to, and then click submit to save all of them.
Copy linkTweet thisAlerts:
@slaughtersNov 09.2007 — First off, how would I make this into a button:

[CODE]for (i=0; i<document.forms.length; i++) {
document.forms[i].submit();
}[/CODE]
[/QUOTE]
Simplest example I can think of:
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;

&lt;head&gt;

&lt;title&gt;Submit all Forms&lt;/title&gt;

&lt;script&gt;
function SubmitAll() {
for (i=0; i&lt;document.forms.length; i++) {
document.forms[i].submit();
}
}
&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;a href="#" onclick="SubmitAll();return false;"&gt;
Click to Submit All forms on this page
&lt;/a&gt;

&lt;/body&gt;
&lt;/html&gt;
If there are any forms on the page this should submit them all.
Copy linkTweet thisAlerts:
@nnhubbardauthorNov 09.2007 — Thanks, I really appreciate your help!
×

Success!

Help @nnhubbard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...