Form confirmation on submit

后端 未结 5 1544
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 03:58

I want to have a confirmation from the user before they submit a form. This will prevent accidental POSTing of the form, that may be incomplete or incorrect.

Here is my

相关标签:
5条回答
  • 2021-01-21 04:28

    I would do an onclick returning false by default, this works for me

    <form action="manager.php?method=update&id=<?php echo $user->id;?>"  method="POST" id="user_update"> 
    <input type='submit' onclick="return confirm('Do you wish to update details?');return false;"/>
    </form>
    
    0 讨论(0)
  • 2021-01-21 04:29

    First of all you should reconsider your approach. Instead of asking whether the user wants to submit a potentially incomplete or invalid form, you should use javascript to prevent this from happening, i.e. perform client-side validation using js. What you are doing is inherently done by clicking submit...

    If however you want to keep your approach, you have to prevent the submit button from actually submitting the data to the specified action, e.g by changing the form action to "#" via javascript and then trigger the submit if ok was clicked with your js-code, e.g. by using a XmlHttpRequest.

    0 讨论(0)
  • 2021-01-21 04:40

    You're doing it the other way round!

    if(confirm("Do you wish to update details?"))
    {
        return 1;
    }
    else
    {
        return 0;
    }
    

    Having said that, your code can be shortened to just one line:

    return confirm("Hit OK to continue, Cancel to... cancel.");
    
    0 讨论(0)
  • 2021-01-21 04:52

    Instead of returning 0 and 1, return true and false. You can actually shorten the function to:

    function confirm_update() {
        return confirm("Are you sure you want to submit?");
    }
    
    0 讨论(0)
  • 2021-01-21 04:55

    Try:

    
    function confirm_update() {
        if(confirm("Do you wish to update details?")) {
            return true;
        }
        else {
            return false;
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题