最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Form confirmation on submit - Stack Overflow

programmeradmin16浏览0评论

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 form element:

<form action="manager.php?method=update&id=<?php echo $user->id;?>" onsubmit="return confirm_update();"  method="POST" id="user_update"> 

This calls my function confirm_update()

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

The problem with the script is that it does not prevent the form from POSTing if the user clicks Cancel in the JavaScript prompt.

How do I correct this so that the user does not accidently submit their form?

Here is a full description of the feature I am trying to implement:

Use Case - "Update Details"

  • User goes to update page
  • User enters details in form fields
  • User hits submit button
  • Confirmation message appears
  • If "Ok" button selected proceed to submit form
  • Else cancel action and stay on current page

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 form element:

<form action="manager.php?method=update&id=<?php echo $user->id;?>" onsubmit="return confirm_update();"  method="POST" id="user_update"> 

This calls my function confirm_update()

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

The problem with the script is that it does not prevent the form from POSTing if the user clicks Cancel in the JavaScript prompt.

How do I correct this so that the user does not accidently submit their form?

Here is a full description of the feature I am trying to implement:

Use Case - "Update Details"

  • User goes to update page
  • User enters details in form fields
  • User hits submit button
  • Confirmation message appears
  • If "Ok" button selected proceed to submit form
  • Else cancel action and stay on current page
Share Improve this question edited Feb 1, 2012 at 11:27 zrvan 7,7531 gold badge24 silver badges23 bronze badges asked Feb 1, 2012 at 11:02 tomaytotomatotomaytotomato 4,02817 gold badges70 silver badges128 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 10

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?");
}

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.");

Try:


function confirm_update() {
    if(confirm("Do you wish to update details?")) {
        return true;
    }
    else {
        return false;
    }
}

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>

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)

  1. 暂无评论