Javascript 如何保护HTML表单不被空白提交
基本上,我使用的是本教程: 一切都正常工作,但我发现有一个流程是,每个人都可以看到Javascript 如何保护HTML表单不被空白提交,javascript,php,html,Javascript,Php,Html,基本上,我使用的是本教程: 一切都正常工作,但我发现有一个流程是,每个人都可以看到.php的URL,在本例中是“URL:”contact_mail.php“ 当有人键入url并按enter键时,是否有方法保护我的表单不被空白提交。 示例:www.mywebsite.com/contact_mail.php 谢谢大家! 使用if(空($\u POST['your\u field'])) 因此,如果post或get查询到达php脚本,empty将检查字段是否为空 比如说: if (empty($_P
.php
的URL,在本例中是“URL:”contact_mail.php“
当有人键入url
并按enter键时,是否有方法保护我的表单不被空白提交。
示例:www.mywebsite.com/contact_mail.php
谢谢大家! 使用if(空($\u POST['your\u field']))
因此,如果post或get查询到达php脚本,empty将检查字段是否为空
比如说:
if (empty($_POST['your_field'])) {
echo 'Field xxx should not be empty';
}
虽然isset会更好,因为如果他们只是去链接,你的POST和GET变量是空的。
所以像这样的东西是万无一失的:
if (!isset($_POST['your_field']) || empty($_POST['your_field'])) {
echo 'Field xxx should not be empty';
}
我不认为GET需要一段单独的代码,但没问题
if (!isset($_GET['your_field']) || empty($_GET['your_field'])) {
echo 'Field xxx should not be empty';
}
表单空白提交您可以使用java脚本验证或jquery验证验证,也可以使用php验证来避免空白表单提交
核心js验证
简单的例子:
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
jquery验证
验证库
例如:
$("#myform").validate({
submitHandler: function(form) {
// some other code
// maybe disabling submit button
// then:
$(form).submit();
}
});
我希望有帮助。说你有以下表格
<form action="savething.php" method="GET" name="mythingform">
<input name="thing1" type="text" />
<input name="thing2" type="text" />
<input type="button" value="Submit" onclick="validateAndSubmit()" />
</form>
在这里,我使用了一个按钮,而不是提交类型的输入。这意味着,在页面提交之前需要发生一些事情,例如
<script>
function validateAndSubmit()
{
var thing1 = document.getElementsByName("thing1")[0];
var thing2 = document.getElementsByName("thing2")[0];
if (thing1.value.length > 0 && thing2.value.length > 0)
{
document.forms["mythingform"].submit();
}
}
</script>
函数validateAndSubmit()
{
var thing1=document.getElementsByName(“thing1”)[0];
var thing2=document.getElementsByName(“thing2”)[0];
if(thing1.value.length>0&&thing2.value.length>0)
{
document.forms[“mythingform”].submit();
}
}
这里的JavaScript函数仅在输入不为空时调用表单上的submit
阻止他人未经许可访问此文件
<?php
if (!isset($_REQUEST['myvariable'] || empty($_REQUEST['myvariable']))
die("Please make sure the form has been submitted properly with all required information");
首先,您可以在客户端的必填字段中使用该属性:
<input type="text" name="mandatory_field" required>
您可以使用验证字段是否已提交。您的验证可以是:
if (!isset($_POST['mandatory_field']) || empty($_POST['mandatory_field'])) {
// print error message or redirect to form page
}
其他情况:
如果所有字段都是必填字段,您可以检查:
如果我在这里使用表单进行各种数据验证:
$errors = [
'empty field' => empty($_POST['field']),
'another error message' => $another_condition
];
if (in_array(true, $errors)) {
$error_message = array_search(true, $errors);
// print or redirect, and you can tell the user what is wrong
}
(1) 如果有人在浏览器中“仅仅输入”URL,应该不会有任何危险-后端代码应该只响应POST,而不是GET(在浏览器中输入URL会使其对给定URL发出GET请求)
(2) 引用的示例代码已经包含客户端验证(包括检查空字段),因此如果有人合法使用您的输入表单,他们将无法发送空白表单
(3) 剩下的就是保护后端代码不受意外或恶意发布空表单(以及任何其他不受欢迎的使用)的影响。示例PHP代码没有任何检查,您应该添加一些检查,比如isset(…)
或empty()
检查在这里的另一个答案中建议) 您的意思是提交空字段吗?首先对表单字段应用jquery客户端验证,而不是在contact_mail.php上应用服务器端验证,以检查任何空白字段making fieldsrequired
应该会有一些帮助(至少在现代浏览器中是这样),但并不完美,但是,您提供的jobExample是否具有检查空表单的验证功能?是的,表单具有验证功能,但当您键入url时,正如我所提到的,空字段会发送到电子邮件地址。这将仅检查POST变量,而不会获取编辑应答器或使用$\u请求
,对于post/Get来说,这是一个相当不错的总括,使用isset也更好,因为他可能会在同一页中提交。谢谢,我已经用isset()和empty()进行了额外的检查,现在一切都很好。@angelo942很高兴它对您有所帮助!
if (in_array(false, $_POST)) {
// print error message or redirect to form page
}
$errors = [
'empty field' => empty($_POST['field']),
'another error message' => $another_condition
];
if (in_array(true, $errors)) {
$error_message = array_search(true, $errors);
// print or redirect, and you can tell the user what is wrong
}