Jump to content

Recommended Posts

I have a form and what I want is for it to do 2 things when the form button is clicked:

 

1) replace the Submit button with a "loading..." image or text

2) show the image/text above for about 5 seconds, then actually submit the form

 

Here's my form:

 

<form method="post" action="">
<input type="hidden" name="amount" value="<?=$raid;?>" />
<input class="btn" type="submit" name="raid" value="Submit" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/272786-help-with-form-onsubmit/
Share on other sites

No java currently. On submit, it just executes the form, which is a php calculation and insert to the DB. I'm hoping to add java. Here's something I found online, which does #1 of what I'm looking to do, but I need help with applying #2 to it:

 

<script type="text/javascript">
(function (d) {
 d.getElementById('form').onsubmit = function () {
   d.getElementById('submit').style.display = 'none';
   d.getElementById('loading2').style.display = 'block';
 };
}(document));
</script>
<form id="form" action="">
 <div id="loading2" style="display:none;"><img src="loading.gif" alt="" />Loading!</div>
 <input id="submit" value="Click!" type="submit" />
</form>

The javascript is right there on my last reply. Here it is again:

 

<script type="text/javascript">
(function (d) {
 d.getElementById('form').onsubmit = function () {
    d.getElementById('submit').style.display = 'none';
    d.getElementById('loading2').style.display = 'block';
 };
}(document));
</script>

 

What I'm asking for is probably about 2-3 lines of additional code. Hardly requiring hiring a professional. If you can't help, no worries.

The javascript is right there on my last reply. Here it is again:

You were talking about Java. Completely different things.

 

What I'm asking for is probably about 2-3 lines of additional code. Hardly requiring hiring a professional. If you can't help, no worries.

You can solve this by doing the following:

 

1) On click, swap out the button with the loader (which is what your code is attempting to do).

2) Set a timeout for 5 seconds using window.setTimeout().

3) In the callback to setTimeout(), submit the form. If you are using jQuery, you can do this with .submit().

 

It will be a little more than 2-3 lines of extra code, probably about 20. But that should point you in the right direction since you don't need to hire a professional.

Edited by haku

Ok, I almost have what I want. I've written this so far, which is working; however, only when the button type="button". It does not work when the type="submit". I need the form to submit the data. What am I missing here?

 

  <script type="text/javascript">
   function submitForm() { // submits form
    document.getElementById("ismForm").submit();
   }
   function btnSearchClick()
   {
    if (document.getElementById("ismForm")) {
  document.getElementById('searchx').style.display = 'none';
	 document.getElementById('loading').style.display = 'block';		
    setTimeout("submitForm()", 5000); // set timout		 
   }
   }
   </script>
<form method="post" id="ismForm" name="ismForm" action="test3.php?x" class="">
<input type="hidden" name="amount" value="100" />
<div id="loading" style="display:none;"><img src="http://article.onlinewebtool.com/wp-content/images/loading.gif" alt="" />Loading!</div>
   <input class="button" onclick="btnSearchClick();" type="button" id="searchx" name="searchx" value="Search" autocomplete="off">
   </form>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.