点击提交表单 [英] Onclick to submit a form

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

问题描述

有一个由 PHP 生成的 HTML 4 过渡页面,用于从单个记录的数据库中编辑数据.用户有两个选择:存储更改或删除记录.我使用带有控件的表单(其中一些是隐藏的):

There is a PHP-generated HTML 4 transitional page that is used to edit data from a database of a single record. The user has two options: to store changes or delete the record. I use a form with controls (some of them are hidden):

<form method="post" action="object_mod.php"><!-- this is another file -->
<!-- inputs follow -->

因为我想处理两个动作,即.删除或保存记录 我在表单上放了两个提交按钮,在 </FORM> 标签之前:

As I want to process two actions ie. delete or save a record I put two submit buttons on the form, before </FORM> tag:

<input type="submit" id="btnSubmit" value="Save">
<input type="submit" id="btnDelete" value="Delete">
</form>

因为用户应该确认删除,所以我添加了以下 onclick 事件:

Because the user should confirm deletion I added the following onclick event:

<input type="submit" id="btnDelete" value="Delete" onclick="javascript:deleteRecordConfirm();">

(我也尝试不使用 javascript:onclick="javascript:deleteRecordConfirm(); return true;"),但它没有提交表单.

(I also tried without javascript: and onclick="javascript:deleteRecordConfirm(); return true;"), but it doesn't submit a form.

JS 函数是

function deleteRecordConfirm(){
  if(confirm('Are you sure to delete?')){
    document.getElementById("field_action").value=-1;
    //document.forms[0].submit();
    return true;
  }
}

这个 field_action 设置为 -1,所以我知道在 object_mod.php 中我想删除记录而不是保存它.

This field_action is set to -1 so I know in object_mod.php that I want to delete record rather than save it.

问题来了,为什么这个表单在删除时不提交?

我认为如果用户禁用 Javascript 提交表单会很好,即使没有确认,这就是为什么我使用 <INPUT TYPE="submit"> 进行删除.这是个好主意吗?我在考虑给两个独立的表单(实际上删除应该只有一个带有记录 id 的隐藏字段)和自己的提交按钮,一个用于删除,另一个用于保存.

I think it would be good if a user has Javascript disabled to submit a form anyway, even without confirmation so that is why I use <INPUT TYPE="submit"> for deletion. Is it a good idea? I was thinking about giving two independent forms (in fact deletion should have only one hidden field with record id) with their own submit buttons, one for deletion and the other for saving.

事实上,该页面可以在某种内部网中运行,我信任的用户可以使用并且我不害怕黑客攻击或其他什么,但也欢迎任何安全说明.

In fact the page will work in some kind of intranet, with users who I trust and I'm not afraid of hacking or something, but any security remarks are also welcome.

(我在 Firefox 19.0 上测试过,Javascript 控制台没有显示错误,w3c 验证器说这是一个有效页面).

(I tested it on Firefox 19.0 and Javascript console shows no errors, w3c validator says it's a valid page).

推荐答案

表单应根据您的代码提交.我唯一发现的是你应该用 /> 终止输入标签.

The form should submit according to your code. The only thing I spot is that you should terminate the input tags with />.

BUT... 这样,即使确认被取消,表单也会被提交.使用 form.onsubmit 处理程序,如果返回 false,表单将不会提交.

BUT... this way, even if the the confirm is cancelled, the form will be submitted. Use the form.onsubmit handler and if that returns false, the form will not submit.

我不认为@B3aT 的答案是正确的,因为这不是无条件地外部化"的最佳方式.许多最简单的就是最好的.

I dont think @B3aT's answer is right in that not unconditionally the best way to "externalise" so to say. Many the the simplest is the best.

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

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