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 Coder
    Join Date
    Oct 2002
    Location
    Oakland, CA
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checkform then another test

    Hi,

    Extreme JavaScript newb here.

    The idea is if they don't select anything from the list and click Remove', they're alerted. If they behave nicely and do select something prior to pressing the 'Remove' button, then it will perform the target = 0 or 1 check.

    It is alerting them if nothing is selected prior to pressing 'Remove', but it is still submitting the form!

    Thanks for any advice on this one!



    <head>
    <script language="javascript">
    function Target(target)
    {
    validMessage="Please select one or more Cities/Zips/Areas to remove";
    dataEntered = new Boolean(true)

    if (document.form2.CITY_LIST.value == ""){
    dataEntered = false;
    }

    if(dataEntered==false){
    alert(validMessage);
    return false;
    }

    else
    {
    if (target == 0) document.form2.action="custsel.asp?re=move";
    document.form2.submit;
    if (target == 1) document.form2.action="custsel.asp?re=set";
    document.form2.submit;
    }
    }
    </script>
    </head>

    <html>
    <form action="custsel.asp" method="post" id=form2 name=form2>
    <select name="CITY_LIST" size="20" multiple>
    <option value="A">City A</option>
    <option value="B">City B</option>
    <option value="C">City C</option>
    etc...
    </select>
    <input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="Target(0)">&nbsp;&nbsp;
    <input type="SUBMIT" value="Reset" id=SUBMIT2 name=SUBMIT2 onClick="Target(1)">
    </form>

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="return Target(0)">  
    <input type="SUBMIT" value="Reset" id=SUBMIT2 name=SUBMIT2 onClick="return Target(1)">
    The onclick function also has to return the "false" value that your validation function returns in order to cancel the submit action.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    New Coder
    Join Date
    Oct 2002
    Location
    Oakland, CA
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha, thanks Roy!

    I thought next I'd try to enhance a line (red) to my Target() function to verify that in fact it is the Remove button being clicked and not the Reset button.

    Question: How to tell which submit button in the form is being pressed? The following code issues an alert if either button is clicked!

    <head>
    <script language="javascript">
    function Target(target)
    {
    validMessage="Please select one or more Cities/Zips/Areas to remove";
    dataEntered = new Boolean(true)
    if (document.form2.SUBMIT1.value == "Remove" && document.form2.CITY_LIST.value == ""){
    dataEntered = false;
    }

    if(dataEntered==false){
    alert(validMessage);
    return false;
    }

    else
    {
    if (target == 0) document.form2.action="custsel.asp?re=move";
    document.form2.submit;
    if (target == 1) document.form2.action="custsel.asp?re=set";
    document.form2.submit;
    }
    }
    </script>
    </head>

    <html>
    <form action="custsel.asp" method="post" id=form2 name=form2>
    <select name="CITY_LIST" size="20" multiple>
    <option value="A">City A</option>
    <option value="B">City B</option>
    <option value="C">City C</option>
    etc...
    </select>
    <input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="Target(0)">
    <input type="SUBMIT" value="Reset" id=SUBMIT2 name=SUBMIT2 onClick="Target(1)">
    </form>

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since you're passing a different value to the submit function depending on which submit button was pressed, just test for that value.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    New Coder
    Join Date
    Oct 2002
    Location
    Oakland, CA
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again for the input, Roy.

    To make things clearer, I've changed the function a bit.

    This works if I select some cities and click Remove. But if I press the Remove All button why am I still being alerted?



    <script language="javascript">
    function Target(target)
    {
    if (target == 0 && document.form2.CITY_LIST.value != ""){
    document.form2.action="custsel.asp?re=move";
    document.form2.submit;
    }
    else{
    alert("Please select one or more Cities to remove");
    return false;
    }

    if (target == 1){
    document.form2.action="custsel.asp?re=moveall";
    document.form2.submit;
    }
    }
    </script>

    <form action="custsel.asp" method="post" id=form2 name=form2>
    <select name="CITY_LIST" size="20" multiple>
    <option value="A">City A</option>
    <option value="B">City B</option>
    <option value="C">City C</option>
    etc...
    </select>
    <input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="return Target(0)">
    <input type="SUBMIT" value="Remove All" id=SUBMIT2 name=SUBMIT2 onClick="return Target(1)">
    </form>
    </html>

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by hyefive
    This works if I select some cities and click Remove. But if I press the Remove All button why am I still being alerted?
    Because your script reads:

    If target equals 0 and CITY_LIST value is not empty do this.
    Else If not the above alert....

    Clicking target 1 without choosing any of the options would be an else!

    Try switching what the first conditional checks for.

    eg:
    Code:
    <script language="javascript">
    function Target(target)
    {
    if (target == 0 && document.form2.CITY_LIST.value == ""){
    alert("Please select one or more Cities to remove");
    return false;
    }
    else{
    document.form2.action="custsel.asp?re=move";
    document.form2.submit;
    }
    
    if (target == 1){
    document.form2.action="custsel.asp?re=moveall";
    document.form2.submit;
    }
    
    
    }
    </script>
    .....Willy

  • #7
    New Coder
    Join Date
    Oct 2002
    Location
    Oakland, CA
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aw man, no! Thanks for pointing out the obvious Willy!

    I'll get the hang of this yet...


  •  

    Posting Permissions

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