提交表格确认 [英] Form confirmation on submit

查看:160
本文介绍了提交表格确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的表单元素:

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

这会调用我的函数 confirm_update()

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

脚本的问题在于它无法阻止如果用户在JavaScript提示中单击取消,则来自POST的表单。

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

推荐答案

而不是返回0和1,返回 true false 。你实际上可以将函数缩短为:

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

这篇关于提交表格确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆