Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prevent page from refreshing when user clicks OK in alert box

    The following Javascript checks to see that all form fields have been filled in and causes an alert to pop up if they have not. This works correctly. However, after the user clicks OK, the page blinks and appears to refresh, and all the previously entered data is lost so that the user has to re-enter everything again. Can this be prevented?

    <script language="javascript">
    var yn;
    var ye;
    var fn;
    var fe;
    var ms;

    function editFields() {
    document.invite_form.formGoodOrNot.value = "Y";

    yn = document.invite_form.yourname.value;
    ye = document.invite_form.youremail.value;
    fn = document.invite_form.friendname.value;
    fe = document.invite_form.friendemail.value;
    ms = document.invite_form.message.value;
    if (yn == "" || yn == " " || ye == "" || ye == " "
    || fn == "" || fn == " " || fe == "" || fe == " "
    || ms == "" || ms == " ")
    {
    alert("You must enter all information");
    document.invite_form.formGoodOrNot.value = "N";
    }

    }
    </script>

    Here is the link to the actual page:

    www.westernoaks.org/invitations/invitation.asp

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah man.. that reason is pretty simple.. try this code instead and let me know:

    <script language="javascript">
    var yn;
    var ye;
    var fn;
    var fe;
    var ms;

    function editFields() {
    document.invite_form.formGoodOrNot.value = "Y";

    yn = document.invite_form.yourname.value;
    ye = document.invite_form.youremail.value;
    fn = document.invite_form.friendname.value;
    fe = document.invite_form.friendemail.value;
    ms = document.invite_form.message.value;
    if (yn == "" || yn == " " || ye == "" || ye == " "
    || fn == "" || fn == " " || fe == "" || fe == " " || ms == "" || ms == " ")
    {
    alert("You must enter all information");
    document.invite_form.formGoodOrNot.value = "N";
    return;
    }
    document.invite_form.submit();
    }
    </script>
    <form action="invitation.asp" method="post" name="invite_form">

    <input type="button" value="Preview Your Invitation" name="Button" onclick="editFields();">
    Last edited by smalldog; 05-11-2007 at 09:03 AM.
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    if (yn == "" || yn == " " || ye == "" || ye == " "
    || fn == "" || fn == " " || fe == "" || fe == " " || ms == "" || ms == " ")

    This simply checks that at least one character (except a single space but not two or more spaces) - even a ?, and/or however inappropriate - has been entered. It is doubtful if such a simple check is worthwhile. At a minimum check that each field has a length of at least 4 and the email fields have a @ and a . That is still a long way off a robust validation.

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you to both of you for your comments. Here are my responses:

    smalldog,

    Clicking the "Preview Your Invitation" button already leads to running editFields() because the function is called right after that. The problem is what happens when the following line is executed and the user clicks OK.

    alert("You must enter all information");

    Is there something I can add to this statement to make things behave differently right after that?

    Philip,

    I actually was aware that this code doesn't check for much. Where can I find a good example of how to create a more robust check? I would like to see how to do this.

    Shirley

  • #5
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Shirleymck, I understand what is the problem problem is, your form is posted, and data lost then ... So, I suggest you how to modify your script. If you will do it, problem should be solved.
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #6
    New Coder
    Join Date
    Jul 2002
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am doing some more testing on this page, so please disregard this question for now. Thanks.


  •  

    Posting Permissions

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