How can I make a single error page with PHP?

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!

Moderator: General Moderators

Post Reply
antoine
Forum Commoner
Posts: 37
Joined: Thu Jul 10, 2008 1:43 pm

How can I make a single error page with PHP?

Post by antoine »

So I have basic knowledge of PHP, but I was wondering if someone could help me with this. Or at least tell me if it's possible.

I have a form on my wesbite. I have a simple validation code in the "form_process.php" that makes sure there is an actual value in each required field. When there isn't, I have:

print
"<meta http-equiv=\"refresh\" content=\"0;URL=some_error_page.php\">";

This works, and if there is an empty field, it goes to the error page. However, I was wondering if there was a way to set it up so that the error page displays a specific error depending on which field(s) were left empty.

Then how would I write the "some_error_page.php" so that it only displays those error messages?

-------------

In a somewhat unrelated question, is there a way of validating (with PHP) so that the validation error is in a <div> or something beside the corresponding input box?

-------------

I also have a CAPTCHA image, which shows up and works fine, but the validation code:

include('captcha/captchac_lib.php');
$Turing_code = $_REQUEST["Turing"];
if ( CheckCaptcha($Turing_code) !=1 )
{
echo "<b><font color=red>The image verification test faild. Please press the Back button of your browser and try again</font></b><br>";
return 1;
}

(where "Turing" is the name of the field that the user inputs the code and "captcha/captchac_lib.php" is the file that makes the code for the image.

...never works, even when the field is empty. This is another reason I was wondering if I could classify the errors. If anyone has used "CAPTCHA Creator PHP Script 1.2", maybe they could share their validation script.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: How can I make a single error page with PHP?

Post by WebbieDave »

I appreciate you trying to keep things tidy but I'm just going to answer your first two question as the captcha one should really be a separate post :) Maybe someone else will swoop in and answer that one later.

What about appending the URL with come error codes:

Code: Select all

print '<meta http-equiv="refresh" content="0;URL=some_error_page.php?errorcode=1,4,7">'
Then some_error_page.php will use explode, foreach and switch to print the corresponding errors. You could put the actual field names in the URL instead of numbers, but using the numbers is safer.

To your second question, you can always keep appending data to a string variable and then print it later:

Code: Select all

<?php
$errMsg = '';
// Validate name
$errMsg = $errMsg . 'Name is missing<br>'; 
// Validate email
$errMsg = $errMsg . 'Email is not valid<br>'; 
// Do a whole bunch of other stuff...
?>
<div><?php echo $errMsg ?></div>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: How can I make a single error page with PHP?

Post by Stryks »

If I understand your question properly, you could easily achieve what you want by submitting the form to itself, with the form processing code at the top of the page.

It runs through, and when it finds an error it sets a flag. If the flag is set then it displays the div in the form next to the problem field. Then you use the header redirect on a successful form submit, otherwise just let the form display with embedded error messages.

You'll have to re-populate the form and so on, but it's a neat way to keep form display and data handling on the same page. Also, redirecting after form 'success' prevents users from 'backing' into your form.

Cheers
antoine
Forum Commoner
Posts: 37
Joined: Thu Jul 10, 2008 1:43 pm

Re: How can I make a single error page with PHP?

Post by antoine »

Stryks wrote:You'll have to re-populate the form and so on, but it's a neat way to keep form display and data handling on the same page. Also, redirecting after form 'success' prevents users from 'backing' into your form.
What exactly do you mean? The user will have to re-enter all the data? Is there a way to prevent that?
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: How can I make a single error page with PHP?

Post by deejay »

I think what you need to get your head around is how POST works. http://uk.php.net/manual/en/reserved.variables.post.php.

whether you use the same page or another you're form details are held in $_POST . you can retrieve them with

Code: Select all

 
 
$variable = $_POST['variable'];
 

and then check that it doesn't exist with

Code: Select all

 
 
if(!$variable){
echo ' you haven\'t entered a variable';
}
 
 
if you want to stop the user having to re-enter information then you have all the details they've entered in $_POST, so from inside the from you could do a check on whether a value for that part of the form exists- if it does then echo it.

Hope this helps
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: How can I make a single error page with PHP?

Post by Stryks »

To clarify, take a look at the following basic example.

Code: Select all

<?php
    if(isset($_POST["active"])) {
        // Form is submitted - validate
        if(!ctype_alpha($_POST['username'])) $flag['username'] = true;   // Just a basic alpha only test to fail
        
        if(!isset($flag)) {
            // no flags are set so no validations have failed - insert processing code here and then ...
            header('Location: http://www.example.com/success.php');
            exit();
        }     
    }
 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>Test Form</title>
    <style>
        .error { background-color: red; color: white; }
    </style>
</head>
<body>
    <form name="frm_test" method="post" action="simple_form.php" >
<?php
    if(isset($flag['username'])) {
?>
        <div class="error">Username must contain letters only</div>
<?php
    }
?>    
        <label for="username">Username: &nbsp;</label><input <?php if(isset($flag['username'])) echo 'class="error" '; ?>type="text" name="username" maxlength="20"<?php if(isset($_POST['username'])) echo ' value="' . $_POST['username'] . '"'; ?>>
        <input type="hidden" name="active" value="true"><input type="submit" name="submit" value="submit">
    </form>
</body>
</html>
It's a form that posts back to itself. If you enter a number into the field it will show an error message and apply some ugly as sin CSS rules.

Basically, by posting to itself, the form acts as a loop, returning to itself over and over until it passes the validation rules you have defined and it can be processed.

When I mentioned having to re-populate the form, I simply meant (as noted by deejay) you just echo the value that the user typed back into the form. I have done just that in the example.

Check it out, pull it apart. Hope it helps.
antoine
Forum Commoner
Posts: 37
Joined: Thu Jul 10, 2008 1:43 pm

Re: How can I make a single error page with PHP?

Post by antoine »

Thank you Stryks... that is very helpful. Thanks a lot.

That alpha only test is nice. Is there somewhere where I can get a list of all the available validation tests?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: How can I make a single error page with PHP?

Post by Stryks »

Well ... you could look up the ctype functions ... though I cant quite remember if it's available on all servers. I think you needed to load the ctype extension on older versions.

Anyhow, they can be somewhat limiting if your looking for portability. A stronger way would be to use regex.

If you're new to regex (regular expressions) there's a board to ask questions on this forum, and an excellent two part regex primer by Chris Corbyn HERE and HERE.

Jump over to that board, have a read of the primer, and if you have any questions, that board is the place.

Cheers.
Post Reply