PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I want to be able to send the user back to the page when the validation fails...eg... they do not enter a valid e-mail address and after they get the error it redirects them back to the page they were on or they can click on a link that takes them back to the page, this way they dont have to click back in the browser... any help would be great. Below is a copy of the code.
<?php
/*
* To tie the script together and prevent direct access to
* example-validation.php and example-confirmation.php
*/
define('IN_EXAMPLE', true);
// To hold error messages
$message = '';
// Checks that the form was submitted
if (isset($_POST['btnSubmit'])) {
/* Runs the validation script, and only returns to the form page if
* validation fails
*/
require_once 'example-validation.php';
}
?>
<html>
<head><title>Form Page</title></head>
<body>
<?php echo $message; ?>
<form method="post" action="example-form.php">
<label>Enter a name: <input type="text" name="txtName" /></label>
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<p><i>Hint: the only acceptable inputs are "George" and "Banjo" (case-sensitive).</i></p>
</body>
</html>
<?php
// This script cannot be accessed directly.
if ( ! (defined('IN_EXAMPLE') && IN_EXAMPLE === true)) {
@include_once 'example-403.php';
exit;
}
// The number of failed validations
$count_failed = 0;
// This is some simple validation to demonstrate the example.
if ( ! (isset($_POST['txtName']) && in_array($_POST['txtName'], array('George', 'Banjo')))) {
// Increments the number of failed validations
$count_failed++;
}
// If there are no failures, the inputs passed validation
if ($count_failed == 0) {
// Includes the confimation page.
require_once 'example-confirmation.php';
/* Stops processing.
* If exit was not here, the form page would be
* appended to the confirmation page.
*/
exit;
} else {
// Defines a message to display when the inputs fail validation.
$message = '<p style="color: red">Please fill in the form properly.</p>';
// The form page will continue to be processed now.
}
?>
<?php
// This page cannot be accessed directly
if ( ! (defined('IN_EXAMPLE') && IN_EXAMPLE === true)) {
@include_once 'example-403.php';
exit;
}
?>
<html>
<head><title>Confirmation Page</title></head>
<body>
<h1>The form was submitted successfully.</h1>
<p>Name: <?php
/*
* It is safe to echo $_POST['txtName'] here because
* it has (supposedly) passed validation
*/
echo $_POST['txtName'];
?></p>
</body>
</html>
<?php
// This is an optional "Access Denied" page.
// Using a 403 page is somewhat more graceful than exiting with no output.
header('HTTP/1.1 403 Forbidden');
?>
<html><head><title>Access Denied</title></head><body></body></html>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 3:21 pm, edited 1 time in total.
<?php
/*
* To tie the script together and prevent direct access to
* example-validation.php and example-confirmation.php
*/
define('IN_EXAMPLE', true);
// To hold error messages
$messages = array();
// Default input values (later, sanitized $_POST inputs)
$inputs = array('txtName' => '');
// Checks that the form was submitted
if (isset($_POST['btnSubmit'])) {
/* Runs the validation script which only returns to the form page if
* validation fails
*/
require_once 'example-validation.php';
}
?>
<html>
<head><title>Form Page</title></head>
<body>
<?php
if (count($messages) > 0) {
// Displays a list of error messages
echo '<ul style="color: red"><li>'.implode('</li><li>', $messages).'</li></ul>';
}
?>
<form method="post" action="example-form.php">
<label>Enter a name: <input type="text" name="txtName" value="<?php echo $inputs['txtName']; ?>" /></label>
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<p><i>Hint: the only acceptable inputs are "George" and "Banjo" (case-sensitive).</i></p>
</body>
</html>
<?php
// This script cannot be accessed directly.
if ( ! (defined('IN_EXAMPLE') && IN_EXAMPLE === true)) {
@include_once 'example-403.php';
exit;
}
// The number of failed validations
$count_failed = 0;
// This is some simple validation to assist the example.
if ( ! (isset($_POST['txtName']) && in_array($_POST['txtName'], array('George', 'Banjo')))) {
// Increments the number of failed validations
$count_failed++;
// Adds a message to the message queue.
$messages[] = 'Name is not valid. Please enter a valid name.';
} else {
// If other validations fail, this will be used to repopulate the form
$inputs['txtName'] = $_POST['txtName'];
// A debugging message.
$messages[] = 'DEBUGGING: txtName was determined to be a valid input.';
}
/* For debugging only.
* Change to false to allow the script to execute normally.
* Change to true to see the persistent inputs work.
*/
if (true) {
$messages[] = 'DEBUGGING: Condition to debug persistent inputs is TRUE. To disable this message, check '.__FILE__.' at line '.__LINE__;
$count_failed++;
}
// If there are no failures, the inputs passed validation
if ($count_failed == 0) {
// Includes the confimation page.
require_once 'example-confirmation.php';
/* Stops processing.
* If exit was not here, the form page would be
* appended to the confirmation page.
*/
exit;
}
// The form page will continue to be processed now.
?>
<?php
// This page cannot be accessed directly
if ( ! (defined('IN_EXAMPLE') && IN_EXAMPLE === true)) {
@include_once 'example-403.php';
exit;
}
?>
<html>
<head><title>Confirmation Page</title></head>
<body>
<h1>The form was submitted successfully.</h1>
<p>Name: <?php
/*
* It is safe to echo $_POST['txtName'] here because
* it has (supposedly) passed validation, but it is
* better to use the sanitized $inputs array
*/
echo $inputs['txtName'];
?></p>
</body>
</html>
example-403.php (Unchanged)
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 3:22 pm, edited 1 time in total.
Thanks for the response, I have everything working in my script except when someone does not validate on the page, what I want to do is redirect it back to the page they were on using the script that I have, not sure how to do this as I am new to php scripting...
As soon as an input fails validation in your script, an error message is output to the browser. If you redirect, how is the user going to see the error message? The example I posted addresses this by storing the error messages in an array, then displaying them later.
By the way, a header('Location: ...') redirect will not work after sending output, so the only other option would be to use a timed META refresh. You could do that, but if a user gets up to get a snack right after clicking the submit button, it will not be obvious that there had been an error when (s)he returns.
Is the form page located in the same directory or at least on the same server as the script you posted? You could give up on the idea of "redirection" altogether and solve the problem with an include. If the validation fails, include the form page and terminate the validation script. You could do the same thing with a single file. Have the form page and confirmation page in the same file. Just use control statements to display the appropriate HTML.
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 3:24 pm, edited 1 time in total.