Page 1 of 1

Add link back to page after Validation fails

Posted: Sun May 03, 2009 12:43 am
by gavinr98
Hello,

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.

Thanks
Gavin.

Code: Select all

 
// Validation
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email<br></font></p>");
}
 

Re: Add link back to page after Validation fails

Posted: Sun May 03, 2009 12:53 pm
by McInfo
Here is one way to do it.

example-form.php

Code: Select all

<?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>
example-validation.php

Code: Select all

<?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.
}
?>
example-confirmation.php

Code: Select all

<?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>
example-403.php

Code: Select all

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

Re: Add link back to page after Validation fails

Posted: Sun May 03, 2009 1:34 pm
by McInfo
Here is the same example with improved error messages and persistent form input values.

example-form.php

Code: Select all

<?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>
example-validation.php

Code: Select all

<?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.
?>
example-confirmation.php

Code: Select all

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

Re: Add link back to page after Validation fails

Posted: Sun May 03, 2009 11:24 pm
by gavinr98
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...

Thanks for all the help
Gavin...

Code: Select all

 
<?php
 
// Receiving variables
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$email = addslashes($_POST['email']);
@$Name = addslashes($_POST['Name']);
@$Company_Name = addslashes($_POST['Company_Name']);
@$Phone = addslashes($_POST['Phone']);
@$Best_Time_To_Contact = addslashes($_POST['Best_Time_To_Contact']);
@$Best_Way_To_Contact = addslashes($_POST['Best_Way_To_Contact']);
@$Message = addslashes($_POST['Message']);
 
// Validation
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email<br></font></p>");
}
 
if (strlen($Name) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid name</font></p>");
}
 
if (strlen($Company_Name) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid Company Name</font></p>");
}
 
if (strlen($Best_Time_To_Contact) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid Best Time To Contact You</font></p>");
}
 
if (strlen($Best_Way_To_Contact) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid best way to contact you</font></p>");
}
 
if (strlen($Message) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid message</font></p>");
}
 
//Sending Email to form owner
$pfw_header = "From: $email\n"
  . "Reply-To: $email\n";
$pfw_subject = "Information Requested by Customer";
$pfw_email_to = "info@xxxxxx.com";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "email: $email\n"
. "Name: $Name\n"
. "Company_Name: $Company_Name\n"
. "Phone: $Phone\n"
. "Best_Time_To_Contact: $Best_Time_To_Contact\n"
. "Best_Way_To_Contact: $Best_Way_To_Contact\n"
. "Message: $Message\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
 
//Sending auto respond Email to visitor
$pfw_header = "From: info@xxxxxx.com\n"
  . "Reply-To: info@xxxxxxx.com\n";
$pfw_subject = "Request for Services";
$pfw_email_to = "$email";
$pfw_message = "Thank you for your request\n"
. "\n"
. "Thank You,\n";
 
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
 
<head>
<title>NetNow - Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#ffffff" text="#000000">
 
<div>
<center>
<b>Thank you <?php print stripslashes($_REQUEST['name']); ?></b>
<br>Your message has been sent
<p><a href="<?php print $continue; ?>">Click here to continue</a></p>
<p><b></a></p>
</center>
</div>
 
</body>
</html>
 

Re: Add link back to page after Validation fails

Posted: Mon May 04, 2009 12:15 am
by McInfo
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.

Re: Add link back to page after Validation fails

Posted: Mon May 04, 2009 11:39 am
by gavinr98
Yes, all the forms are in the same directory, do you have a link or an example of how to add the include file to the script?

Thanks
Gavin...

Re: Add link back to page after Validation fails

Posted: Mon May 04, 2009 1:31 pm
by McInfo
gavinr98 wrote:do you have a link or an example of how to add the include file to the script?
See the second and third posts in this topic.

Edit: This post was recovered from search engine cache.