简体   繁体   English

PHP表单验证。 不在同一页面上

[英]PHP Form validation. Not on the same page

I am creating a simple contact form and the form is going through client side validation with jQuery plus server side validation with PHP in case user disables Javascript on their browsers. 我正在创建一个简单的联系表单,如果用户在浏览器中禁用了Javascript,该表单将通过jQuery进行客户端验证以及PHP进行服务器端验证。

Although there are many source code examples out there that can process and display with a single page, I haven't seen many that separates them into: form.php and validator.php , for example to do such task. 尽管有很多可以在单个页面上处理和显示的源代码示例,但我还没有看到将它们分成以下形式的很多示例:例如form.phpvalidateator.php来完成此任务。

I have this form.php file that are mostly written in html for marking up the form with some php codes that will receive/display the error or success message retrieved from the validator.php . 我有这个form.php文件,该文件主要是用html编写的,用于使用一些php代码标记表单,这些代码将接收/显示从validator.php检索到的错误或成功消息。 Now, the problem I am having is linking these two so they talk to each other without complaining. 现在,我遇到的问题是将这两个链接在一起,以便他们彼此交谈而不会抱怨。

"form" attribute has the action assigned to validator.php and within validator.php I have one of the function as follows: “ form”属性具有分配给validator.php的操作,在validator.php中,我具有以下功能之一:

if (isset($error)) {
    $msg = "<p>Please enter valid information.</p>";
    require ("form.php"); 
}

And, on form.php I declared require ("validator.php"); 并且,在form.php上,我声明了require ("validator.php"); and using this $msg variable from validator.php to display the message but the browser complains that the $msg is undefined even though the validation had its run and has the string defined. 并使用来自validateator.php的 $ msg变量显示消息,但浏览器抱怨$ msg是未定义的,即使验证已运行且已定义了字符串。

By the look of it, I presume these two php files are not linked properly. 从外观上看,我认为这两个PHP文件未正确链接。 Anyone has an idea to the solution? 有人对解决方案有想法吗?

require is actually the same thing as if you copied "validator.php" into the "form.php" file, so this should not be a problem. require实际上与将“ validator.php”复制到“ form.php”文件中相同,因此这应该不是问题。 Variables share the same scope in included files as their "parents". 变量在包含的文件中与其“父对象”共享相同的作用域。 However, it is not a good idea to include "validator.php" in "form.php" and then call require("form.php") from its code - it will be an infine loop! 但是,在“ form.php”中包含“ validator.php”,然后从其代码中调用require("form.php")并不是一个好主意-这将是一个infine循环!

PS And, if you are using require , you can't call it "not on the same page". PS而且,如果使用的是require ,则不能将其称为“不在同一页面上”。 It is the same page, it is the same URL for the user. 这是同一页面,它是用户的相同URL。 It is two different files, that's true. 这是两个不同的文件,这是正确的。

you cannot use require validator.php and then direct your page to validator.php . 您不能使用require validator.php ,然后将页面定向到validator.php Once you require a page that page is included within the current page and can be used as part of the page. 一旦需要页面,该页面将包含在当前页面中,并且可以用作该页面的一部分。 Think of it as copy pasting the code from validator.php where you have used require('validator.php') . 可以将其视为粘贴代码,将代码从validator.php粘贴到使用了require('validator.php') So just set action="" in form.php and validate it accordingly. 因此,只需在form.php设置action=""并相应地对其进行验证。

Also its better to use require_once('validator.php') . 同样,最好使用require_once('validator.php')

Yes,there is a problem with linking between the two files. 是的,两个文件之间的链接存在问题。

When you receive an error you run:- 收到错误时,您将运行:-

if (isset($error)) {
    $msg = "<p>Please enter valid information.</p>";
    require ("form.php"); 
}

This code resides in validator.php and when you load form.php in it, it again loads validator.php in it which again resets the $msg variable as it has nothing posted to it. 该代码驻留在validator.php中,当您在其中加载form.php时,它将再次在其中加载validator.php,这将再次重置$ msg变量,因为它没有任何内容。

One solution can be:- 一种解决方案可以是:

Use form.php to get input from the user. 使用form.php获取用户输入。 Post the output to validator.php which would validate the input and redirect to the page form.php with a $msg set that would be displayed by form.php as an error msg to user. 将输出发布到validateator.php,它将验证输入并重定向到具有$ msg集的form.php页面,该表单将由form.php显示为对用户的错误msg。

Problem resolved with using session variable from the view (form.php) as in 使用视图中的会话变量(form.php)解决了问题,如下所示

<?php if (isset($_SESSION['msg'])) echo $_SESSION['msg']; session_unset(); ?>

where msg variable is used for error/success that will be printed inside form and I had to declare session_start(); 其中msg变量用于错误/成功,该错误/成功将在表单内打印,我必须声明session_start(); on both view(form.php) and controller(validator.php) at the very top. 在最顶部的view(form.php)和controller(validator.php)上。

On one of validation function inside controller(validator.php) I did the following 在controller(validator.php)内部的验证功能之一上,我执行了以下操作

$msg = "<p>Email Successfully Sent!</p>";
$_SESSION['msg'] = $msg;
header('Location: index.php');
exit;

For my purpose, it didn't require any of include , require , require_once to link them. 就我的目的而言,它不需要includerequirerequire_once来链接它们。

PS form.php replaces index.php in my actual file structure. PS form.php替换了我实际文件结构中的index.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM