Reputation: 10540
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?
Upvotes: 0
Views: 458
Reputation: 10540
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.
P.S. form.php replaces index.php in my actual file structure.
Upvotes: 0
Reputation: 33511
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!
P.S. 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.
Upvotes: 1
Reputation: 3272
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.
Upvotes: 1
Reputation: 1017
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')
.
Upvotes: 1