SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    two buttons in a form

    Code:
    <script type="text/javascript"> 
    
    function sure(){
    msg = "Are you sure?";
    return confirm(msg);
    }
    
    </script>
    
    <form method='post' action='preview.htm'>
    
    <textarea name='myMessage'></textarea>
    <input type='submit' value='Preview'>
    <input type='submit' value='Submit' 
    onClick="this.form.action='action.htm';return sure()">
    
    </form>
    I have a code like the above.

    There are two buttons, i.e, preview and submit at http://dot.kr/test/22.htm
    If you click the button "preview," it will go to preview page.
    If you click the button "submit," an alert with confirm button and cancell button will pop.

    And
    if you click the confirm button, it will go to action.htm.
    if you click the cancell button, it will stay there.


    I have a problem with the code above.


    If you click the button "preview" for the first time, it will go to "preview.htm."

    But,
    if you click the button "preview" after you click the button "cancell" in the alert, it goes to action.htm instead of preview.htm although you click the button "preview."


    How can I make it to always go to preview.htm when you click the button "preview?"

  2. #2
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi joon,

    When you set the form's action, it stays set. So when this line of code runs:

    Code:
    this.form.action='action.htm'
    The action will stay "action.htm" regardless of which button you press. So, you can either set the form.action back to "preview.htm" when the user clicks preview:

    Code:
    <script type="text/javascript"> 
    
    function sure(){
      var msg = "Are you sure?";
      return confirm(msg);
    }
    
    </script>
    
    <form method='post' action='preview.htm'>
    
    <textarea name='myMessage'></textarea>
    <input type='submit' value='Preview'
    onclick="this.form.action='preview.htm'">
    <input type='submit' value='Submit' 
    onclick="this.form.action='action.htm';return sure()">
    
    </form>
    Or, you can change the function sure() so that it only changes the action if the user is sure:

    Code:
    <script type="text/javascript"> 
    
    function sure(form){
      var msg = "Are you sure?";
      var reply = confirm(msg);
      if (reply) {
        form.action='action.htm';
      }
      return reply;
    }
    
    </script>
    
    <form method='post' action='preview.htm'>
    
    <textarea name='myMessage'></textarea>
    <input type='submit' value='Preview'>
    <input type='submit' value='Submit' 
    onclick="return sure(this.form)">
    
    </form>
    hth,
    Douglas
    Last edited by DougBTX; May 19, 2007 at 16:51.
    Hello World

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi, Douglas.

    Your first code seems not to work, but I don't mind it because your second code works fine.

    Thank you, Doublas.


    I have one more question adding to this issue.

    I like to make the code above work fine with another function, ie, if a user clicks with no entering, it will alert "Message, please."

    The following code is one of my trials for it.
    Although it doesn't work correctly, it will show what I want.

    Code:
    <script type='text/javascript'>
      function validate(f) {
      if (f.box.value.length == 0) {
      alert("Message, please");
      return false;
      }
      return true;
      }
    </script>
    
    
    <script type="text/javascript"> 
    function sure(form){
      var msg = "Are you sure?";
      var reply = confirm(msg);
      if (reply) {
        form.action='action.htm';
      }
      return reply;
    }
    </script>
    
    <form method='post' action='preview.htm' 
    onsubmit='return validate(this)'>
    
    <textarea name='myMessage'></textarea>
    <input type='submit' value='Preview'>
    <input type='submit' value='Submit' 
    onclick="return sure(this.form)">
    </form>

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joon View Post
    Your first code seems not to work, but I don't mind it because your second code works fine.
    Aah, sorry, it was missing a quotation mark after preview.htm.

    Douglas
    Hello World

  5. #5
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX View Post
    it was missing a quotation mark after preview.htm.
    Douglas
    Your first code works fine now.





    By the way, Can I make the following with your help?

    (1) if a user clicks the button "preview" or "submit" without anything entering into the textArea, it will alert "Message, please."

    (2) if a user clicks the button "preview" with something entering into the textArea, it will go to preview.htm.

    (3) if a user clicks the button "submit" with something entering into the textArea, an alert with confirm button and cancell button will pop.


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
  •