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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Validation/form submission problem

    I am trying to validate a form with three dropdown boxes. If the second two don't change when the user submits, and alert shows up. Right now, the alert shows up, but the form still submits. I don't know what's wrong.
    Code:
    <HTML>
    <HEAD>
    <TITLE>Untitled Document</TITLE>
    <SCRIPT type="text/javascript">
    function box() {
      if (window.document.form_1.select_2.selectedIndex == 0 || window.document.form_1.select_X.selectedIndex == 0)
      {
        alert("Where do you think you're going?");
        return false;
      } else {
        return true;
      }
    }
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    <form name="form_1"  action="http://www.localheroclothing.com/store/customer/cart.php" method="post" onSubmit="box();">
    <select name="select_1">
            <option>-- Products --</option>
            <option value=1>T-Shirt</option>
            <option value=2>Longsleeve T-Shirt</option>
    </select>
    <select name="select_2">
             <option>Please Select a Color</option>
             <option>red </option>
             <option>green </option>
           </select>
    <select name="select_X">
             <option>Please Select a Size</option>
             <option>Large</option>
             <option>Small</option>
           </select><INPUT type="submit" value="Add to Cart" >
    </BODY>
    </HTML>
    If you'd like to try it, it will send you to my shopping cart, but not products will be added. The code looks fine to me, but something doesn't work. How do I keep it from submitting?

  • #2
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok... found the problem... here's the corrected code. Exlpanation after:
    Code:
    <HTML>
    <HEAD>
    <TITLE>Untitled Document</TITLE>
    <SCRIPT type="text/javascript">
    function box() {
      if ((window.document.form_1.select_2.selectedIndex == 0 )||( window.document.form_1.select_X.selectedIndex == 0))
      {
        alert("Where do you think you're going?");
        return false;
      } else {
        return true;
      }
    
    }
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <form name="form_1"  action="http://www.localheroclothing.com/store/customer/cart.php" 
    method="post" onSubmit="return box();">
    <select name="select_1">
            <option>-- Products --</option>
            <option value=1>T-Shirt</option>
            <option value=2>Longsleeve T-Shirt</option>
    </select>
    <select name="select_2">
             <option>Please Select a Color</option>
             <option>red </option>
             <option>green </option>
           </select>
    <select name="select_X">
             <option>Please Select a Size</option>
             <option>Large</option>
             <option>Small</option>
           </select><INPUT type="submit" value="Add to Cart" >
    </form>
    </BODY>
    </HTML>
    Okay... now remember. In order for the form submission to be stopped... it has to resemble "return false"... when the "box()" executes... the only thing it returns is "false", so your submission thingy previously looked like.. onSubmit="false"... so sticking a "return" before the box() function solves the problem.

    I tested it, it works!
    Last edited by jaywhy13; 07-06-2005 at 06:10 PM.
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished

  • #3
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I'll try it..Thanks.
    Last edited by Local Hero; 07-06-2005 at 06:20 PM.

  • #4
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Local Hero
    I'll try it..Thanks.
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished


  •  

    Posting Permissions

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