SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    933
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Submit button confirmation?

    I'm noob at javascript

    How should I do this?

    I have this form with submit button.
    What I want is when I press the submit button the confirmation windows with
    YES or NO should appear first.

    If I press yes It should go ahead and do the normal process.
    And If I will press NO it should stay and don't do anything.

    How to do this in javascript?

    Thanks in advanced.

  2. #2
    SitePoint Member mixo's Avatar
    Join Date
    Jul 2012
    Location
    United States of Russia
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <a href="javascript: if (confirm('Continue?')) { window.location.href='google.com' } else { void('') }; ">I Agree</a>
    change the google.com to your site/link whatever, and the I Agree is the link title

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mixo View Post
    change the google.com to your site/link whatever, and the I Agree is the link title
    Please don't mix JavaScript in with your HTML code.

    Here is a sample form:

    Code html4strict:
    <form id="registerUser">
        ...
        <p><input type="submit" value="Register User"></p>
    </form>

    We can attach a function on to the onsubmit event of the form, which is triggered by the submit button.

    script.js
    Code javascript:
    var form = document.getElementById('registerUser');
    form.onsubmit = function () {
        // this method is cancelled if window.confirm returns false
        return window.confirm('Are you sure that you want to submit this form?');
    }

    Here's how it might look when put together:

    Code javascript:
    <html>
    ...
    <body>
    <form id="registerUser">
        ...
        <p><input type="submit" value="Register User"></p>
    </form>
     
    <script type="text/javascript" src="script.js"></script>
    </body>
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member mixo's Avatar
    Join Date
    Jul 2012
    Location
    United States of Russia
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Please don't mix JavaScript in with your HTML code.

    Here is a sample form:

    Code html4strict:
    <form id="registerUser">
        ...
        <p><input type="submit" value="Register User"></p>
    </form>

    We can attach a function on to the onsubmit event of the form, which is triggered by the submit button.

    script.js
    Code javascript:
    var form = document.getElementById('registerUser');
    form.onsubmit = function () {
        // this method is cancelled if window.confirm returns false
        return window.confirm('Are you sure that you want to submit this form?');
    }

    Here's how it might look when put together:

    Code javascript:
    <html>
    ...
    <body>
    <form id="registerUser">
        ...
        <p><input type="submit" value="Register User"></p>
    </form>
     
    <script type="text/javascript" src="script.js"></script>
    </body>
    </html>
    but what if the user press ok? what wheres the redirect link?

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mixo View Post
    but what if the user press ok?
    If the OK button is pressed, that will result in a true value from the confirm dialog. The onsubmit event is only cancelled when false is returned, so with true being returned the form will submit as per normal.

    Quote Originally Posted by mixo View Post
    what wheres the redirect link?
    The original poster says nothing about a redirect link. I think that might be a misunderstanding that is occurrring.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •