473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help with "Prevent Users from Submitting a Form Twice"

Hello,

I read an article on "how to prevent users from submitting a form twice." at the url http://www.4guysfromrolla.com/webtech/100406-1.shtml

I was able to freeze the web page but was not able to unfreeze the web page. I am using PHP with javascript. Nothing happens when my code executes the "unfreeze" js function. I tested this js function and it only worked after I disabled the 2 php header() functions below. The JS unfreeze function just stops working with the php header functions enabled. There were no errors. Only the web page still freezes.

I hope someone can give me advice to help me solve this issue. Thanks in advance.

Below is my code in short version:


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3.  
  4. $hearingform = '
  5. <html>
  6. <head>
  7.  
  8. <style type="text/css">
  9. .....
  10. </style>
  11.  
  12. <div align="center" id="FreezePane" class="FreezePaneOff">
  13. <div id="InnerFreezePane" class="InnerFreezePane"> </div>
  14. </div>
  15.  
  16. <script type="text/javascript">
  17.  
  18. function FreezeScreen(msg) {
  19. scroll(0,0);
  20. var outerPane = document.getElementById("FreezePane");
  21. var innerPane = document.getElementById("InnerFreezePane");
  22. if (outerPane) outerPane.className = "FreezePaneOn";
  23. if (innerPane) innerPane.innerHTML = msg;
  24. }
  25.  
  26. function unFreezeScreen() {
  27. var outerPane = document.getElementById("FreezePane");
  28. outerPane.className = "FreezePaneOff";
  29. }';
  30.  
  31. //---------------------------------------------------------
  32. // some code to pull data from the database here...
  33. //---------------------------------------------------------
  34.  
  35. // unfreeze the webpage
  36. echo '<script type="text/javascript">unFreezeScreen();</script>';
  37.  
  38. /*send data to an excel file*/
  39. header("Content-type: application/csv");
  40. header("Content-Disposition: attachment; filename=$filename");
  41.  
  42. echo $data;
  43.  
  44. exit;
  45. } //END post IF
  46.  
  47. print $hearingform;
  48.  
  49. ob_flush();
  50. ?>
  51.  
Jul 20 '12 #1
1 27498
Claus Mygind
571 512MB
First keep in mind that php only runs on the web server and javaScript only runs on the browser.

You stream out the web page with php. When sending out non-php code (html, js or css) you either wrap that code with "echo" or you close the php segment with "?>" then re-open php when you want to run more php code. For example in the code below notice where I inserted the "?>" and "<?php" in your shown code. Since you have not shown all your code, there may be other places where this has to be done.

Next you may want to consider testing your code in FireFox using the addon "firebug" and "firephp" so you can actually step through your code one line at time and review the result of each line.

Since the action you seek is to prevent the user from submitting the form twice, this then most likely is something you would want to do on the browser with javaScript only (as it appears you are attempting to do). However if you are attempting to prevent a denial of service request because someone has hacked your site, this is something you would have to do on the server with php.

Below your code I show a simpler way to prevent submission of the form.


Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. ob_start();
  4.  
  5. $hearingform = '
  6. ?>
  7.  
  8. <html>
  9. <head>
  10.  
  11. <style type="text/css">
  12. .....
  13. </style>
  14.  
  15. <div align="center" id="FreezePane" class="FreezePaneOff">
  16. <div id="InnerFreezePane" class="InnerFreezePane"> </div>
  17. </div>
  18.  
  19. <script type="text/javascript">
  20.  
  21. function FreezeScreen(msg) {
  22. scroll(0,0);
  23. var outerPane = document.getElementById("FreezePane");
  24. var innerPane = document.getElementById("InnerFreezePane");
  25. if (outerPane) outerPane.className = "FreezePaneOn";
  26. if (innerPane) innerPane.innerHTML = msg;
  27. }
  28.  
  29. function unFreezeScreen() {
  30. var outerPane = document.getElementById("FreezePane");
  31. outerPane.className = "FreezePaneOff";
  32. }';
  33.  
  34.  
  35. <?php 
  36. //---------------------------------------------------------
  37. // some code to pull data from the database here...
  38. //---------------------------------------------------------
  39.  
  40. // unfreeze the webpage
  41. echo '<script type="text/javascript">unFreezeScreen();</script>';
  42.  
  43.  
  44. /*send data to an excel file*/
  45. header("Content-type: application/csv");
  46. header("Content-Disposition: attachment; filename=$filename");
  47.  
  48. echo $data;
  49.  
  50. exit;
  51. } //END post IF
  52.  
  53. print $hearingform;
  54.  
  55. ob_flush();
  56. ?>
Instead of using the standard "Submit" button on your form do the following:

Expand|Select|Wrap|Line Numbers
  1. <form ... onsubmit="return false">
  2.  
this will prevent the form from being submitted using the enter key. Next use a plain button to submit the form with validation. Use the validation to test if the form has already been submitted.

Expand|Select|Wrap|Line Numbers
  1. <input type="button" value="Submit" 
  2. onclick="if (validateForm(this.form)) {this.form.submit())" />}
  3.  
Use a simple flag to determine if the form has been submitted. flag = false; when you stream out the page or when new or changed data is displayed. Then set the flag to true when form has been submitted. This on/off action would prevent double submitting of the form.
Jul 22 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Laermans_k | last post by:
Hi, Does anyone have a solution to use the <input type="file" id="filechooser"> and the <input type="submit" id="submitbutton"> in 1 button click? I've already tried to create a javascript...
4
by: Koen | last post by:
Hi all, At work I created a database which is really helpful. The database is used by approx 15 users. Everything worked great, until I added some 'scoreboard' forms and reports. I get the...
0
by: BGS | last post by:
I have a web site (www.on-the-matrix.com) that displays photos in a "slide show" format on ASPX pages that run in an inline frame on an ASP page (or in a separate pop-up window). The ASPX pages...
2
by: Vinod I | last post by:
Hi Team, When I tryed following code, I am getting the Runtime Error as "The View State is invalid for this page and might be corrupted." Exception Details: System.Web.HttpException: The View...
4
by: roxanaislam | last post by:
Submitting Form by pressing the "ENTER" key -------------------------------------------------------------------------------- Hi: I have a search form in my application which has 4 dropdown...
1
by: monudjn | last post by:
Hi I am implementing ajax in portal. Using ajax i am able to updating the content of portlets asynchronously. However i am facing a problem The Problem: While submitting the form i am getting...
1
by: jan.marien | last post by:
we have a table with jobs and a table with job_history information. Users can define jobs and let them run every X minutes/hours , like a cronjob. The jobs table has the following trigger:...
7
Fary4u
by: Fary4u | last post by:
Hi how to find the return value if it's wrong parameters I need to STOP form on false statement with out submitting form () <input onchange="preview(this); this.form.submit()"> I’ve tried...
2
scubak1w1
by: scubak1w1 | last post by:
Hello, I am building a form that collects some data about a file and throws it into a PosgreSQL database and also allows the user to upload and process the file using PHP's $_FILES... i.e.,...
12
sueb
by: sueb | last post by:
I want to have a separate form for adding records to my database (so that I can lock the main ID field and prevent users' accidentally changing it). I've been able to create this form, and put a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.