Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Submit Form Data Without Bothering User

An entire form can be submitted to the server without the website user having to know about it. Collect the entire form's data with the JavaScript FormData() function and send the data to a script on the server with Ajax.

In general, submitting a form takes the browser to the URL of the script on the server where the data is being submitted to. If you prefer to implement any other action instead, submitting the form via Ajax is most likely to be at least part of the solution.

I'll show you how. But first an illustration.

Here is a testing form for the illustration. When you test the form, it will respond with an alert containing the information you provided on the form.

Test form

Your name

Favorite colors
Blue
Red
Yellow
Green

Your idea

I think you will like the (less than 20 lines) JavaScript that submits the form.

The JavaScript has one customization, the id of the form tag.

It submits the entire form to the URL specified in the form tag's action URL. Because it is Ajax, it will submit the form only to a script on the same server where the web page is at.

Put the JavaScript on the web page with the form. It's location can be anywhere JavaScript can go. Placing it near the bottom of the source code immediately above the closing </body> tag should work just fine.

<script type="text/javascript">
function SubmitTheForm()
{
   var FormID = "my-form";
   var http = new XMLHttpRequest();
   if(!http) {return;}
   http.onreadystatechange = function()
   {
      if(http.readyState == 4 & http.status == 200)
      {
         alert(http.responseText);
      }
   }
   http.open("POST",document.getElementById(FormID).action,true);
   http.send( (new FormData(document.getElementById(FormID))) );
}
</script>

The my-form on the first line of the function needs to be replaced with the id of the form to be submitted.

The function has 12 lines of code. On the 8th line, you'll see the alert(http.responseText); code.

After the form information has been sent to the script on the server, alert(http.responseText); gives the user an alert containing the information the server script responded with.

You may change alert(http.responseText); to anything else that is valid JavaScript.

As an example of something that can be changed, you can disable the line so the script provides no response. Or, the alert can just say "Thank you!", if that is what you want. Another example of what the alert(http.responseText); code can be changed to is redirecting the browser to another URL.

To cause the JavaScript to run when the submit button is tapped, the submit button code has the onclick="SubmitTheForm()" attribute.

Here is the code for the test form you used near the beginning of this article.

<form id="my-form" method="post" action="/possibilities/demo/issue1323dump.php">
<p>
Your name
<br><input type="text" name="your_name" style="width:100%; border:1px solid #ccc; padding:.5em; border-radius:.3em; box-sizing:border-box;">
</p>
<p>
Favorite colors
<br><input type="checkbox" name="your_favaorite_colors[]" value="blue"> Blue
<br><input type="checkbox" name="your_favaorite_colors[]" value="red"> Red
<br><input type="checkbox" name="your_favaorite_colors[]" value="yellow"> Yellow
<br><input type="checkbox" name="your_favaorite_colors[]" value="green"> Green
</p>
<p>
Your idea
<br><textarea type="text" name="your_idea" style="width:100%; height:.6in; border:1px solid #ccc; padding:.5em; border-radius:.3em; box-sizing:border-box;"></textarea>
</p>
<p style="margin-bottom:0;">
<input type="button" value="Submit Button" onclick="SubmitTheForm()" style="width:100%; box-sizing:border-box;">
</p>
</form>

As you see in the example, put the onclick="SubmitTheForm()" attribute into the submit button tag of your form. When the button is tapped, the attribute tells the JavaScript to submit the form.

Bringing it all together: To implement the system, put the JavaScript into your web page source code and insert the onclick="SubmitTheForm()" attribute into your form's submit button tag. Finish it by updating the JavaScript with the form tag's id value.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC