如何查看表单元素是否未发布 [英] How to see if a form element was not posted

查看:32
本文介绍了如何查看表单元素是否未发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中电子邮件是可选的.控制是否有一个复选框.如果未选中该复选框,则电子邮件文本框将被禁用,因此不会在提交时发布.然而,在下一页,如果我有如下所示的代码,如果电子邮件文本框被禁用,它会给我一个错误.

I have a form where e-mail is optional. To control that there is a checkbox. If that checkbox is unchecked, the e-mail textbox would be disabled and therefore not posted on submit. However, on the next page, if I have code like as shown below, it gives me an error if the e-mail textbox is disabled.

if (isset($_POST['submit'])) 
{ 
    $_SESSION["email"]      = $_REQUEST['YourEMail'];
    ....
}

为了解决这个问题,除了将其值设置为空字符串之外,我还在提交之前以编程方式启用了禁用的电子邮件文本框.代码如下所示.

To get around that problem, I progammatically enable a disabled e-mail textbox just before submitting besides setting its value to an empty string. The code for that is shown below.

document.getElementById('YourEMail').disabled = false
document.getElementById('YourEMail').value = ''

然而,仍然存在一个烦人的问题,那就是,如果用户返回原始页面,电子邮件文本框会被启用,因为我在提交表单之前就启用了它有问题.但是,我希望在这种情况下禁用它.我怎样才能做到这一点?或者,如何在下一页中看到电子邮件已被禁用,因此甚至没有尝试阅读 $_REQUEST['YourEmail']?

However, one annoying problem remains, which is that, if the user goes back to the original page, the e-mail textbox is enabled, since I enabled it problematically just before submitting the form. However, I want it to be disabled in that case. How, can I achieve that? Alternatively, how in the next page, I could see that e-mail box was disabled and therefore not even try to read $_REQUEST['YourEmail']?

推荐答案

如果字段#YourEMail"是可选的,您可以检查 PHP 中是否存在.无需使用 JS 启用/禁用该字段.

if the field "#YourEMail" is optional you can check if exists in PHP. There is no need for enable/disable the field using JS.

if (isset($_POST['submit'])) 
{
    if (isset($_REQUEST['YourEMail']) && !empty($_REQUEST['YourEMail'])){
        $_SESSION["email"] = $_REQUEST['YourEMail'];
    }
}

这篇关于如何查看表单元素是否未发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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