简体   繁体   中英

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.

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.

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 . 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:

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

And, on form.php I declared 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.

By the look of it, I presume these two php files are not linked properly. 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. 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!

PS And, if you are using require , you can't call it "not on the same page". It is the same page, it is the same URL for the user. It is two different files, that's true.

you cannot use require validator.php and then direct your page to 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') . So just set action="" in form.php and validate it accordingly.

Also its better to use 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.

One solution can be:-

Use form.php to get input from the user. 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.

Problem resolved with using session variable from the view (form.php) as in

<?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(); on both view(form.php) and controller(validator.php) at the very top.

On one of validation function inside controller(validator.php) I did the following

$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.

PS form.php replaces index.php in my actual file structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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