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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help w/ Drop Down Alert

    I'm looking for a drop-down validator to make sure that a value is selected. For example, if value=none show alert; otherwise, submit. I found a site that has something very similar to what I'm looking for:

    http://www.thefixor.com/help/formstyle/dropalert.html

    But the only difference is that the code on this specifies it to go to the value if selected. For example, if selected go to http://www.site.com/value and all that I want it to do is submit.

    Can someone help?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Something like this?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>Untitled</title>
    
    <script>
     function checkDropDown(selId) {
      if(document.getElementById(selId).value != '0') {
       return true;
      }
      else {
       alert('Please select an item from the drop down');
        return false;
       }
     }
    </script>
    
    </head>
    <body>
    
     <form method="post" action="" onsubmit="return checkDropDown('selBox');">
      <select name="selBox" id="selBox">
       <option value="0">--SELECT ITEM FROM DROP DOWN--</option>
       <option value="http://www.yahoo.com">yahoo.com</option>
       <option value="http://www.google.com">google.com</option>
       <option value="http://www.msn.com">msn.com</option>
      </select>
      <input type="submit"/>
     </form>
    
    </body>
    </html>
    If you change the name/id of the select, you'll need to change the parameter in the call to checkDropDown(...) in the onsubmit handler.

    You may also want to fill in the action attribute of the form as well as give the submit input a name/value.

    Hope that helps,
    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I made some modifications to it, but it's not working (no alert). I'm using this within a php file, should that make a difference? What's wrong with the code?


    Code:
    <script>
     function checkDropDown(shipping) {
      if(document.getElementById(shipping).value != 'none') {
       return true;
      }
      else {
       alert('Please select an item from the drop down');
        return false;
       }
     }
    </script>
    <form method='POST' action='checkout2.php' onsubmit='return checkDropDown('shipping');'>
    <br>
    <select size='1' name='shipping' id='ff' align='left'>
              <option selected value=none>Select Shipping</option>		
              <option value=0.00>UPS Ground-FREE</option>
              <option value=10.00>UPS1-$10.00</option>
              <option value=20.00>UPS2-$20.00</option>
            </select><br><br><input id='submit' type='submit' value='Continue'></form>
    Last edited by mcmuney; 07-13-2004 at 07:17 PM.

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lots! lol, but no, putting it in a PHP file makes no difference.

    Code:
    <script>
     function checkDropDown(shipping) {
      if(document.getElementById(shipping).value != 'none') {
       return true;
      }
      else {
       alert('Please select an item from the drop down');
        return false;
       }
     }
    </script>
    <form method='POST' action='checkout2.php' onsubmit='return checkDropDown("shipping");'>
    <br>
    <select size='1' name='shipping' id='shipping' align='left'>
     <option selected value=none>Select Shipping</option>		
     <option value=0.00>UPS Ground-FREE</option>
     <option value=10.00>UPS1-$10.00</option>
     <option value=20.00>UPS2-$20.00</option>
    </select><br><br><input id='submit' type='button' value='Continue' onClick='validate()'></form>
    I've highlighted the changes I made in red. One question I do have concerns validate(). Does this function submit the form? If it does, then it's all good. But if not, then you may still encounter a problem as you're not submitting the form any longer (that submit button is a NOT a submit button, but rather just a regular button).

    Hope that helps,
    Sadiq.

  • #5
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I actually modified my last post that shows the submit button type='submit' and the validate() removed.

    I made the few changes that you showed in red, but in php I can't use ("), they are replaced with ('). Also, I changed the id='shipping'.

    Still no alert, HELP!

    Thanks,
    Sharif

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why can't you use double quotes? Because that's the problem.

    onsubmit='return checkDropDown('shipping');'

    If you're using ONLY single quotes, these are the strings you are creating.

    Post your PHP code so we can see it..

    Sadiq.

  • #7
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    function checkDropDown(oForm, selName)
    {

    var oSel = oForm[selName];
    var isOk2Send = false;
    for (var i = 0; i < oSel.length; i++)
    {
    if (oSel.options[i].selected)
    isOk2Send = true;
    }

    if (! isOk2Send)
    alert('Please select an item from the drop down');
    return (isOk2Send)

    }
    </script>

    </head>
    <body>

    <form method="post" action="" onsubmit="return checkDropDown(this, 'selBox');">

    Vinny
    Where the world once stood
    the blades of grass
    cut me still


  •  

    Posting Permissions

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