验证 php 表单 - 如何在表单上显示错误消息
我一直收到来自表单的空白提交,并且我知道使用 javascript 进行验证(“必需”验证)并不总是有效,因为垃圾邮件机器人可以关闭 javascript。 所以我一直在寻找使用 php 验证我的 php 表单的方法。
因此,经过一段时间的研究......我发现这会起作用:
if (!$_POST['name'] )
{
die('Please fill in your name');
}
不过,在我点击提交按钮后,这条消息“请填写您的姓名”显示在不同的页面上。现在理想情况下,我希望该消息显示在表单所在的页面上,尽管我正在努力寻找一种方法来做到这一点。 (请原谅我缺乏 PHP 知识)。
我需要 Ajax 吗? 我假设我需要在表单页面上添加一个 div 来显示错误消息?我如何让php在表单页面上显示错误消息? IE:我需要在我的 php 流程表单中放入什么(以及上面的代码?)。
如果您需要我发布/添加更多详细信息,请询问,因为我将感谢您的帮助!
=================================================== =================== 更新
这是我的 php 流程表单:
<?
session_start();
include("verification_image.class.php");
$image = new verification_image();
if (($image->validate_code($_POST['validate']) ? "true" : "false") == "false") {
header('Location: http://www.domain.com/fail.htm');
exit;
}
if (!$_POST['rgerger'] )
{
die('You did not complete all of the required fields');
}
if(!empty($_POST['email'])){ die('Stop Spamming'); }
$to = "[email protected]";
$from = $_POST['ervberster'];
$subject = "I Want to Advertise";
$sBodyNew = '<style type="text/css">
<!--
.style {
font-family: Arial;
font-size: 12px;
color: #4D241E;
}
body {
background-image: url();
background-color: #F1EAE4;
}
.style1 {font-size: 14px}
-->
</style>
<p> </p>
<table width="420" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td><table width="100%" border="0" cellpadding="8" cellspacing="0" bgcolor="#E7D3AF"
class="style">
<tr>
<td colspan="2" valign="top"><div align="center"><strong><span class="style1">I
Want to Advertise on AccommodationMozambique.co.za</span><br>
.................................................</strong><br>
</div></td>
</tr>
<tr>
<td width="32%" valign="top"><div align="left"><strong>Date Submitted</strong>
</div></td>
<td width="68%" valign="top">'. date("F j, Y, g:i a") .'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Name</strong></div></td>
<td valign="top">'.$_POST['name'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Email</strong></div></td>
<td valign="top">'.$_POST['ervberster'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Telephone</strong></div></td>
<td valign="top">'.$_POST['werergrggef'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Name of Lodge</strong></div></td>
<td valign="top">'.$_POST['hneyjyttyh'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Town / City</strong></div></td>
<td valign="top">'.$_POST['gedghethth'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Enquiry</strong></div></td>
<td valign="top">'.$_POST['rervberer323'].'</td>
</tr>
</table></td>
</tr>
</table>
';
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($to, $subject, $sBodyNew, $headers);
header('Location: http://www.domain.com/success.htm');
?>
I've been receiving blank submissions from my form and I know that validating ("required" validation) using javascript isn't always effective because spambots can turn off javascript.
So I've been looking for ways to validate my php form using php.
So after a but of research ... I found that this will work:
if (!$_POST['name'] )
{
die('Please fill in your name');
}
Though, this message 'Please fill in your name' shows on a different page after I've hit the submit button. Now ideally, I'd like that message to display on the page where the form is, though I'm struggling to find a way to do this. (Please excuse my lack on knowledge on php).
Would I require Ajax?
I assume I would need to add a div on the form page where I want the error message to display? How would I ask php to display the error message on the form page? IE: What would I need to put in my php process form (along with the code above?).
If you need me to post / add any more detail, please ask, as I'd appreciate your help!
======================================================================
UPDATE
Here is my php process form:
<?
session_start();
include("verification_image.class.php");
$image = new verification_image();
if (($image->validate_code($_POST['validate']) ? "true" : "false") == "false") {
header('Location: http://www.domain.com/fail.htm');
exit;
}
if (!$_POST['rgerger'] )
{
die('You did not complete all of the required fields');
}
if(!empty($_POST['email'])){ die('Stop Spamming'); }
$to = "[email protected]";
$from = $_POST['ervberster'];
$subject = "I Want to Advertise";
$sBodyNew = '<style type="text/css">
<!--
.style {
font-family: Arial;
font-size: 12px;
color: #4D241E;
}
body {
background-image: url();
background-color: #F1EAE4;
}
.style1 {font-size: 14px}
-->
</style>
<p> </p>
<table width="420" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td><table width="100%" border="0" cellpadding="8" cellspacing="0" bgcolor="#E7D3AF"
class="style">
<tr>
<td colspan="2" valign="top"><div align="center"><strong><span class="style1">I
Want to Advertise on AccommodationMozambique.co.za</span><br>
.................................................</strong><br>
</div></td>
</tr>
<tr>
<td width="32%" valign="top"><div align="left"><strong>Date Submitted</strong>
</div></td>
<td width="68%" valign="top">'. date("F j, Y, g:i a") .'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Name</strong></div></td>
<td valign="top">'.$_POST['name'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Email</strong></div></td>
<td valign="top">'.$_POST['ervberster'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Telephone</strong></div></td>
<td valign="top">'.$_POST['werergrggef'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Name of Lodge</strong></div></td>
<td valign="top">'.$_POST['hneyjyttyh'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Town / City</strong></div></td>
<td valign="top">'.$_POST['gedghethth'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Enquiry</strong></div></td>
<td valign="top">'.$_POST['rervberer323'].'</td>
</tr>
</table></td>
</tr>
</table>
';
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($to, $subject, $sBodyNew, $headers);
header('Location: http://www.domain.com/success.htm');
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您提到的,是的,JavaScript 可以被关闭 - 不仅仅是垃圾邮件机器人,任何人都可以关闭。 JavaScript 是一种客户端语言,因此验证发生在客户端。将安全性建立在希望您的客户按照您的意愿行事的基础上是不好的。永远不要相信用户输入。
话虽这么说,您的 PHP 验证之路是正确的,除了执行 die() 会导致您的页面在打印表单之前停止执行。执行此操作的常见方法是执行类似以下操作:
如果用户输入没有任何问题,它将打印“您的数据正常!”,否则它将打印遇到的所有错误的列表。那可能是这样的
一次只出现一个错误会带来糟糕的用户体验,因为您可能最终不得不提交表单 5 次,因为每次修复一件事时,都会出现一个新错误。
As you mentioned, yes, JavaScript can be turned off - not just by spambots, by anybody. JavaScript is a client-side language, so the validation happens on the client-side. Basing security on hoping that your client does what you want, is bad. Never trust user input.
That being said, you're on the right path with your PHP validation, except doing a
die()
will cause your page to stop executing before you have even printed your form. A common way to do this is doing something like thisThis will print "Your data is OK!" if nothing is wrong with the user input, otherwise it will print a list of all errors encountered. That could be something like
Getting just one error at a time gives a bad user experience, as you might end up having to submit your form 5 times, because every time you fix one thing, a new error comes up.
如果您将同一页面用于多个表单,最好的方法是添加隐藏输入:
比在 PHP 中:
当然,您应该使用不同的且更复杂的面向对象设计(例如具有
validate( 方法)的类)
和getError()
),但这应该是您开始的好地方。If you're using the same page for multiple form, the best way would be adding hidden input:
Than in PHP:
Of course you should use different and little more complicated object oriented design (such as class with methods as
validate()
andgetError()
), but this should be good place to start for you.这取决于您提交表格的方式。假设您要提交页面本身,然后在页面顶部添加如下内容:
然后,在页面的 html 中,您希望显示警告的位置添加:
另外,请注意我的验证语法: if (!isset($_POST['name']))... 这将返回 true 没有变量 $_POST['name'],而 if(!$ _POST['name]) 只会如果在某个时刻设置 $_POST['name'] = false,则返回 true;
让您记住 ![item] 表示“项目为假”,!isset([item]) 表示“变量不存在,而 [item]=='' 表示该项目存在,并且这是一个空字符串。这些都是单独的东西。
It depends on how you are submitting the form. Presuming that you're submitting the page itself, then at the top of the page add something like this:
and then, in the html of the page, where you'd want the warning to be displayed, you'd add:
Also, note my validate syntax: if (!isset($_POST['name']))... this will return true there is not variable $_POST['name'], whereas if(!$_POST['name]) will only return true if, at some point, you set $_POST['name'] = false;
Make you you keep in mind that ![item] means "item is false", !isset([item]) means "the variable doesn't exist, and [item]=='' means that the item exists, and that it's an empty string. These are all separate things.