Results 1 to 10 of 10

Thread: Update or Cancel?

  1. #1

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333

    Update or Cancel?

    I have the following code:

    Code:
    <form name="rar" method=post action="update.asp" onsubmit="return validate()">
    
    <input name="update" type="submit" value="Cancel">
    <input name="update" type="submit" value="Update" >        
    
    </form>
    how can I detect if the user has pressed Cancel or Update within the validate function (i.e. onsubmit="return validate()")?

    Cheers!

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    make cancel a normal button (not submit) and make it run a different function which does something different. If you want it to clear the form make it a reset button.
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    I still want the form to submit even though I pressed the cancel button... but I do not want the validation code to execute...

    Is that possible?

    It would be great if I could have something like this in the validation function:
    Code:
    if (submit button is update){
    
      do validation stuff
      if (validation went OK){
        return true
      }
      else{
        return false
      }
    
    }
    else{
      return true//so that the form can submit...
    }
    Think this is probably not the best way to do it, but the ASP page that this is going to expects the request.form("update") = "Cancel"

    to be true if the cancel button was pressed :P

    The "if (submit button is update){" part is the bit I am having trouble with...

    Cheers

    [added later - I think the problem is that the value of the submit button is not given until the submite has taken place. Any ideas if this is correct?]
    Last edited by chuddy; Dec 8th, 2003 at 01:25 PM.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Quite correct, I don't think there's a cross-browser way to find the submit button pressed. Which means you should move your validation code (or parts thereof) to an onclick handler of the normal submit button.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    but if I do this then how can I specify that I do not want the form to submit if the validation fails?

    <form name="rar" method=post action="update.asp" onsubmit="return validate()">

    i.e. I need an equivalent to the "return" in the above code...

    hmmm... think I may end making the cancel button a "go back" button :P

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    how about this:
    Code:
    var bool = true
    if (button was submit)
        do validation
        if (validation went ok)
        else
            var bool = false
        end if
    end if
    if (bool == true) //If cancel was pressed or validation passed ok.
        document.MyFrm.submit() //This will submit the form.
    end if
    make both buttons normal buttons. This function will submit the form anyway.
    Have I helped you? Please Rate my posts.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Do return validate() in the onclick of the submit button. If validate returns false, the click has no effect.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    just make one function called from onclick of the update button, dont bother with the function at all when pressing cancel. he said he wanted it to submit without validating so it doesnt need to be part of a function.

  9. #9

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    Thanks for all the replies!

    All very useful stuff.

    I didn't know that it was possible to submit the form just by calling it's submit() method... great stuff.

    Again, thanks Acidic, Cornedbee and davebat!

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I often have problems submitting a form with submit(). In particular, I think IE won't submit a form via the submit() method if it has an absolute target URL.

    Or something like that.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width