SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Pointiest Petitions carrotflowers's Avatar
    Join Date
    Jul 2003
    Location
    Suburbs of DC (MD)
    Posts
    595
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    redirect onsubmit of form

    Hi guys,
    I'm sure this is pretty basic stuff but my JS is not up to snuff, as it were. After some extensive searching of this forum, I did figure out how to solve my problem. However I am still curious about why my attempt did not work, so if anyone would like to shed some light: I created a form. I tied to the form's onsubmit action a validator that would send you to a different page if your login was valid or not. Here is my code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function checkPass() {
    if (document.form1.user.value == "user" && document.form1.pass.value == "password") {
    alert("good");
    document.form1.action = "http://google.com";
    }
    else {
    alert("bad");
    document.form1.action = "http://yahoo.com";
    return false;
    }
    }
    </script>
    </head>

    <body>
    <form name="form1" id="form1" action="#" onsubmit="checkPass();">
    <input type="text" size="25" id="user" name="user" />
    <br />
    <input type="password" size="25" id="pass" name="pass" />
    <input type="submit" value="Login" />
    </form>
    </body>
    </html>
    My question is, why didn't this work? If I changed my submit button to a regular button, then called the function from the button's onclick, it works. What I want to know is, is there something about onSubmit that doesn't work with the window.location redirection?

    Thanks!

  2. #2
    SitePoint Addict Guimauve's Avatar
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do not return false to the onSubmit event the form will submit even if you think you have redirected to the next page. Since your form action is # that means that it will come right back where you were.

    Charles

  3. #3
    Pointiest Petitions carrotflowers's Avatar
    Join Date
    Jul 2003
    Location
    Suburbs of DC (MD)
    Posts
    595
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you saying do this instead?

    <form name="form1" id="form1" onsubmit="checkPass();return false;">

    Because the redirect still doesn't happen. I just don't understand why the redirect works if ties to the onclick of a regular button, but the exact same function as shown above will not work when called from onsubmit.


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
  •