SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making sure a form is submitted before leaving page

    Is it possible to prevent a user (or at least warn them with a confirm box) from browsing away from a page, or closing the window, before a form is submitted?
    - Mike
    http://www.georgiaoffroad.com

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, what i would do is submit the form with javascript and then redirect the user. start by having the "submit", instead call your function:

    <input type="button" id="btnSubmit" onClick="yourFunction()" value="submit" />


    Then, in your javascript, create a function for your sequence of events:

    function yourFunction() {
    frmFormName.submit();
    alert("Form has been submitted!");
    document.location = "wherever.htm";
    }

    I'm not positive this will work but give it a try...
    Last edited by makeda; Feb 16, 2002 at 16:27.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your example just throws up an alert box when the form -is- submitted. Using your code, if the user were to alt-f4 the window no confirm() would display to warn the user and anything in the form box would be lost.

    I saw an example via viewsource at Delphi forums. It basically created a hidden feild with a value of 0. The only time this value would change was on the form onSubmit function (it would change the hidden feild to 1). All this revolves around an onbeforeunload event in the javascript tag (see code below). When the page tries to unload, the value of the hidden field is checked - if 0 display the warning, if 1, don't. Again the only way this feild is changed is with the form submit routine.

    Now. It's a great idea in theory, but there is a major glitch. If you try and close the browser before changing any of the form elements, the warning is displayed. However, the first time you enter some text in a textbox or change a drop down menu and the warning routine is rendered useless from there out. That sort of defeats the purpose. I've viewed source to verify that the hidden feild does not change after altering form elements (nor should it).

    Code:
    <SCRIPT LANGUAGE=JavaScript FOR=window EVENT=onbeforeunload>
    <!-- Beginning of JavaScript --------
    var strMsg = 'Danger Will Robinson!' 
    if (document.callentry.whoathere.value==0) {
    window.event.returnValue = strMsg;
    document.callentry.submitcallnotes.focus()
    }
    // -- End of JavaScript code -------------- -->
    </SCRIPT>
    
    <form name="callentry"  onClick="document.callentry.whoathere.value=1;" onsubmit="return CheckForm(this)">
    <input type=hidden name=whoathere value=0>
    <input type=submit name=submitcallnotes  onClick="document.callentry.whoathere.value=1;"></form>

  4. #4
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, guess i misunderstood your question. well, it sounds like the solution you have will work fine. i would suggest setting the value server-side:

    <? php
    if($submit == "submitCallNotes") {
    $iTestVar = 1;
    }
    else {
    $iTestVar = 0;
    }
    ?>
    ....

    <form name="callentry" onsubmit="return CheckForm(this)">
    <input type=hidden name=whoathere value="<?=$iTestVar ?>">
    <input type=submit name=submitcallnotes value="submitCallNotes"></form>
    Last edited by makeda; Feb 19, 2002 at 10:32.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •