[英]PHP form validation on same page - is this logic correct?
我正在尝试构建一个简单的注册表单,该表单将通过一些检查,然后将数据保存到数据库中。 最初,只有注册表格和处理表格,而未经验证的表格数据已保存在数据库中。 当我尝试对过程进行验证时,麻烦就开始了。
<Edit>
这里只有三个目标:
对于#1,发布到php self似乎是理想的选择,因为它为我避免了很多额外的编码,并且对用户具有不必再次键入所有内容的优点。
对于#2,验证仅限于现在检查空白字段,仅此而已。 这是一张注册表格,正在收集用户数据-完全不对用户进行身份验证。
对于#3,我设计了一种将数据发送到另一个php的方法,这不再是问题。
问题是逻辑。 为什么分开工作的零件放在一起会失效? 我对流程应用的逻辑中是否有错误?
其中一条评论谈到了elseif,我也尝试过。 我没有在代码中出现任何错误-没有解析错误,没有语法错误,没有致命错误-但也没有发生任何事情-只是刷新了注册表。 系统的各个部分分别在测试页上工作,但是当与完整表单放在一起时,它将不起作用。
</Edit>
在SE上,我找到了将表单发布到php self的方法,并在发布后的示例代码中进行了尝试。 它按预期工作,一切似乎都很好,因此我将其添加到了表单页面。 但是在实际的表单页面上,除了重新加载之外,它什么也没做。
再次,在SE上,我发现了一个帖子,展示了如何将所有错误捕获到数组中并显示出来。 它似乎可以在一个示例文件上工作,我在变量注册页面中添加了对变量名进行适当更改的代码。 如果没有用户登录,并且用户单击“提交”,则应显示捕获到的空字段错误。 没有。 即使所有字段都留为空白,也会显示任何错误。 此后一切都崩溃了。
现在发生的一切就是重新加载了注册表-错误或没有错误!
我已经尝试了很多事情,以至于我不再确定自从昨晚以来我一直在试图做的事情就是代码现在正在做的事情。 因此,我仅从逻辑和相关问题开始。
这是每个阶段的逻辑和问题。
<php session_start() ;?>
<?php
//check if the user is logged in
if (isset($_SESSION['validuser'])) {
//catch this first - before user spends time filling out the 12 fields!
//send to message page saying "you cannot register - you are already a member and logged in"
//WORKING PERFECTLY - CONFIRMS FORM POSTING TO SAME PAGE CORRECTLY!
//time for other checks....
}
else (!isset($_SESSION['validuser']) && isset($_POST['submit'])) {
//if there is no logged in user and form has been submitted, start checking the fields to see if any fields are empty
//collect all errors in array and display?
//direct back to the form with appropriate messages, form fields retained
//exit here? or no?
//focus now has to pass to the form again - need any code for this? or will it happen automatically?
}
if isset($_POST['submit'])) {
//Should this part be within the else block?
//If it is out side, will it get rendered on load?
//if there are no errors proceed to process the form
//there are further checks that need talking to the database
//once those checks and approvals are over the data gets saved to the database
}
?>
<html>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method = "POST">
<!-- 12 fields to be filled out by user -->
<input type = "submit" name = "submit" value = "submit" />
</form>
</body>
</html>
我想念什么? 请记住,这是我要清理的工作流程。 现在,原始代码已变得如此繁琐,以至于我不想在这里公开它-很有可能我将获得有关输入卫生和输出转义的很多建议! 在此开发环境中,我故意省略了那些位,在这些环境中,只有我才能访问表单和数据库。 在这一点上进行消毒和逃逸只会增加混乱。
就是说,一旦我拥有正确的工作流程,我也将添加这些位:-)
我将其重组如下:
if (isset($_SESSION['validuser']))
{
//user is authenticated
}
elseif (!isset($_SESSION['validuser']) && isset($_POST['submit']))
{
//user is not authenticated, proceed
if (isset($_POST['submit'])) //checking if form was submitted
{
//process 12 input fields
}
else
{
//form wasn't submitted, display error
}
}
这是一个应根据您的描述而起作用的解决方案。
<?php
session_start();
// If we have a "valid users"
if (isset($_SESSION['validuser']))
{
// Display stuff for registered users
}
else // If we don't have a valid user
{
// If there was data submitted to the server
if (isset($_POST))
{
// Handle validation for ... whatever
// print_r($_POST); To see data
}
}
?>
<html>
<body>
<!-- Don't display the form to "valid users". -->
<?php if (!isset($_SESSION['validuser'])): ?>
<form action="<?=$_SERVER['PHP_SELF']?>" method = "POST">
<!-- 12 fields to be filled out by user -->
<input type = "submit" name = "submit" value = "submit" />
</form>
<?php endif; ?>
</body>
</html>
您基本上是对的,我会这样做的
if (isset($_SESSION['validuser'])) {
}else {
if isset($_POST['submit'])) {
//Check errors here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.