提交表格确认

tom*_*ato 3 javascript forms confirm onsubmit

我希望在用户提交表单之前得到用户的确认.这样可以防止意外发布表单,这可能是不完整或不正确的.

这是我的表单元素:

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

这称我的功能 confirm_update()

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

该脚本的问题在于,如果用户Cancel在JavaScript提示符中单击,则不会阻止表单进行POST .

如何更正此问题,以便用户不会意外地提交表单?

以下是我试图实现的功能的完整描述:

用例 - "更新详细信息"

  • 用户进入更新页面
  • 用户在表单字段中输入详细信息
  • 用户点击提交按钮
  • 出现确认消息
  • 如果选择"确定"按钮,则继续提交表格
  • 否则取消操作并保持当前页面

Roh*_*bhu 10

而不是返回0和1,返回truefalse.您实际上可以将功能缩短为:

function confirm_update() {
    return confirm("Are you sure you want to submit?");
}
Run Code Online (Sandbox Code Playgroud)