Page 1 of 1

How do I alter this code to display custom error pages?

Posted: Sun Oct 23, 2011 6:39 am
by jimbojones5678
Hi everyone, :D

I am a beginner web designer and a novice php user. I am a regular forum user and understand that you should always do a search and try to find the answer yourself but I have done that and didn't get any results (or I didn't understand them!).

So I ask for some assistance - either you can tell me the code or link me to the correct answer.

I am working on a website that I have built a enquiry form for and it has a php script for it. I have managed to get the form running properly and got the reCaptcha working correctly (with a custom error page) but I do not understand how to add custom error pages for incomplete email addresses, empty email addresses, or for a blank form? I tried using the same tactic I used for the reCaptcha error page but it didn't work - probably because I was guessing and just tried it in a bunch of different places.

The error msg that displays now is just a plain white page with the msg - Email address is invalid appearing at the top left.
While this is suitably functional it doesn't really look like much so I want to build a custom page for each error that could happen.

So the code is below - if anyone can point a beginner in the right direction I would be super happy. :mrgreen:

if you need anymore code or other details just tell me and I will post them up.

The url of the form page is http://www.sunsolutionshomeimprovement. ... sales.html

This is the code I think you will need - I got it from a demo php script that I have modified. As I said I don't have much clue what I'm doing so I just posted it exactly how it looks - if I over did it I apologize... :oops:

68.// Validate email field.
69.
70.if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
71.{
72.
73.if(preg_match("/(%0A|%0D|\n+|\r+|:)/i",$_REQUEST['email'])){$errors[] = "Email address may not contain a new line or 74.a colon";}
75.
76.$_REQUEST['email'] = trim($_REQUEST['email']);
77.
78.if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}
78.
79.)
80.
81.// Check referrer is from same site.
82.
83.if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}
84.
85.// Check for a blank form.
86.
87.function recursive_array_check_blank($element_value)
88.(
89.
90.global $set;
91.
92.if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
93.else
94.
95.
96.foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}
97.
98.)
99.
100.)
101.
102.recursive_array_check_blank($_REQUEST);
103.
104.if(!$set){$errors[] = "You cannot send a blank form";}
105.
106.unset($set);
107.
108.// Display any errors and exit if errors exist.
109.
110.if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}
111.
112.if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}
113.
114.// Build message.

I don't think there is anymore that is relevant?

Re: How do I alter this code to display custom error pages?

Posted: Sun Oct 23, 2011 12:34 pm
by social_experiment
jimbojones5678 wrote:While this is suitably functional it doesn't really look like much so I want to build a custom page for each error that could happen.
A common method is to have any error message displayed on the same page as your form so when the user enters invalid data (per your specifications) they will receive an error message and can fix it without navigating back to the form. You can use something like Javascript which will handle any validations in 'real-time' so if you call a method for an onblur event, a check will be done as soon as the user leaves that specific field. However, javascript can be disabled on the user side so it's good practise to have a server-side check inplace for such an occasion.

Code: Select all

 // your form here
<?php
 if (isset($_POST['submit_btn'])) {
    // check the data submitted
    $error = myCheckFunction();
    if (is_array($error)) {
        // print error array
    }
    else {
       // no error found, process form.
    }
 }
?>
This example demonstrates how you can have the error messages on the same page. myCheckFunction() would for example accepts your form values as arguments, check them for validity and if an error is found, add that value to an array. When the function is called and returns an array there must be some error so you echo it to the browser.

Hth.

Re: How do I alter this code to display custom error pages?

Posted: Sun Oct 23, 2011 10:13 pm
by jimbojones5678
OK........ 8O

I really have no idea what you just said, or how to implement it?

Am I supposed to just stick that code in somewhere or is that just an example?

I have absolutely no clue about Javascript or how to write it or use it.

Re: How do I alter this code to display custom error pages?

Posted: Sun Oct 23, 2011 11:29 pm
by social_experiment

Code: Select all

 // your form here
<?php
 if (isset($_POST['submit_btn'])) {
    // check the data submitted -- your code here
    $error = myCheckFunction();
    if (is_array($error)) {
        // print error array
    }
    else {
       // no error found, process form.
    }
 }
?>
The code you have looks like it validates the form so you can place it in the area just after submission

Re: How do I alter this code to display custom error pages?

Posted: Mon Oct 24, 2011 1:12 am
by jimbojones5678
OK, thanks. :)

I'm not real sure where the submit part is on the php form?

Re: How do I alter this code to display custom error pages?

Posted: Mon Oct 24, 2011 9:46 am
by social_experiment
It's the button that the visitor can click on to process the form

Re: How do I alter this code to display custom error pages?

Posted: Mon Oct 24, 2011 1:05 pm
by egg82
here's what I do. It's not as elegant as an on-page error handler, but it works:

Code: Select all

<?
echo('<form action="./" method="post" id="redir">');
echo('<input type="hidden" name="db_error" value="'.mysql_error().'">');
?>
</form>
<script language="JavaScript" type="text/javascript">
<!--
document.getElementById('redir').submit();
//-->
</script>
<?
exit();
and on index.php:

Code: Select all

if(isset($db_error) and $db_error != ""){
	echo('<font color="#FFFF00"><marquee bgcolor="#FF0000">'.$lang_index_dberror.strip_tags($db_error).'</marquee></font>');
}
if(isset($error) and $error != ""){
	echo('<font color="#FFFF00"><marquee bgcolor="#FF0000">'.$lang_index_error.strip_tags($error).'</marquee></font>');
}
if(isset($message) and $message != ""){
	echo('<font color="#FFFFFF"><marquee bgcolor="#0000FF">'.strip_tags($message).'</marquee></font>');
}