Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error checking questions 1

Status
Not open for further replies.

cyberspace

Technical User
Aug 19, 2005
968
GB
I'm fairly new to PHP so please excuse me!

I have written a script for my employers website that takes information from a form and then emails it to an account handler.

There was plenty of code on teh internet for this so i was able to pick it up pretty quickly.

I would like to incorporate some basic error handling. I am able to get it to work by simply using if statements....ie

if (empty($name) || empty($email) || empty($etc)) {
header( "Location: $errorurl" );
exit ;

This forwards to a generic page that says simply "there are fields empty, please use the back button and fill in all fields"

I would like to expand this so that there is a list of missing fields on the error page, but i'm not too sure how to go about it?

Any links or tips would be most appreciated!

Thanks.

'When all else fails.......read the manual'
 
test each item separately and add an error message to a string
i.e.

Code:
$msg = "";
if (empty($_POST['field1'])) $msg .= "<br/>You must supply field1";

then if you must use redirects, start a session, assign the value of $msg to a session variable and in the resultant page echo it back out
Code:
session_start;
$_SESSION[['msg'] = $msg;
header ("Location: ...");

and in the form file
Code:
<? session_start();
if (!empty($msg)) echo "<div style=\"color:red\">$msg</div>";
?>
NORMAL FORM CODE HERE

or best of all, install and use html_quickform from the pear repository.
 
Thanks!!

To be honest i'd rather not use redirect's i would prefer the particular field text to turn red...eg missing your name out and trying to submit would make "name" turn red.

I'm still very new to PHP so simple is good at the moment...

If several feilds are left out, wout this code:

$msg = "";
if (empty($_POST['field1'])) $msg .= "<br/>You must supply field1";

generate several individual messages? or one long one showing all missing fields?

'When all else fails.......read the manual'
 
oh it might be worth pointint out also that it is an html page with the form on, and then when you submit it the data is passed to a php script.

Would it all need to be in one file for this to work?

'When all else fails.......read the manual'
 
it does not have to be in one file but both pages need to be php pages (i.e. have a php extension) rather than html (unless you have set your webserver to process straight html through php too.

the scriptlet i posted above will generate one long message at the top. each "failure" point will be on a separate line. you can turn each failed label red pretty easily but it is a wee bit more complex than the code i posted. i'd do it by altering the css class of the offending fields.

but overall i really would use html_quickform. it's really easy to get up and running and then you don't need to worry about validation so much.

 
Thanks again! I will do that then, and just leave the code in black at the top, it's better than the idea i had anyway.

I will also change the extension of the HTML page to PHP - although there isnt any PHP code in there at all...i presume that doesnt matter?

I have no access to the host asides from FTP - is html_quickform client side or server side?

'When all else fails.......read the manual'
 
Having a bit of bother.

For the moment I want to try it with a redirect so that I can follow your example.

in the php script that runs when you submit the form I have put:

msg = "";
if (empty($_POST['contact'])) $msg .= "<br/>You must supply your name";

followed by:

session_start;
$_SESSION[['msg'] = $msg;
header ("Location: $errorurl");

In the error page i have put:

session_start();
if (!empty($msg)) echo "<div style=\"color:red\">$msg</div>";

however when leaving the contact field blank and submitting, nothing happens.

What am i doing wrong?

'When all else fails.......read the manual'
 
sorry. my fault. change this

Code:
if (!empty($msg)) echo "<div style=\"color:red\">$msg</div>";
to
Code:
if (!empty($_SESSION['msg'])) echo "<div style=\"color:red\">$msg</div>";
 
damn. done it again. pressed submit too early

proper posting should have been:

sorry. my fault. change this

CODE
if (!empty($msg)) echo "<div style=\"color:red\">$msg</div>";
to
CODE
if (!empty($_SESSION['msg'])) echo "<div style=\"color:red\">$_SESSION['msg']</div>";
[/code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top