Making Error Messages Not Display First Time

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 4052 Views - Last Post: 06 September 2012 - 12:59 AM Rate Topic: -----

#16 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 04:10 PM

I would probably do the following to correct the $fail problem.

1. Add to functions.php and create a slightly more advanced error system:

$GLOBALS['fail_msg'] = '';

function failed($string) { $GLOBALS['fail_msg'] = $string; }
function get_error() { return $GLOBALS['fail_msg']; }



2. In your main PHP file, since you've already included functions.php, you change the error output to:

<tr><td colspan="2">Sorry the following errors were found<br />
	in your form: <p><font color=red size=1><i><?php echo get_error(); ?></i></font></p></td></tr>



3. Lastly, change your process.php error checking section to:

if(isset($_POST['submit']))// only do this if submit has been pressed, thus error messages will not show before hitting signup for first time
{
    $GLOBALS['fail_msg'] .= validate_forename($forename);
    $GLOBALS['fail_msg'] .= validate_surname($surname);
    $GLOBALS['fail_msg'] .= validate_password($password);
    $GLOBALS['fail_msg'] .= validate_age($age);
    $GLOBALS['fail_msg'] .= validate_email($email);
}



And that should fix the problem of $fail not working.
Was This Post Helpful? 0
  • +
  • -

#17 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 04:14 PM

Maybe if I walk through how I think the program is working it will be easier for someone to spot where it is going wrong:

So it starts when I put localhost/phpHTMLTest.php into my broswer. At this point phpHTMLTest.php is run. It gets to this line:
<tr><td colspan="2">Sorry the following errors were found<br />
	in your form: <p><font color=red size=1><i><?php echo $fail?></i></font></p></td></tr>


And should display the php variable $fail. This is in process.php. So it looks in process.php for the value of $fail. process.php hasn't been run yet, so $fail is nothing so nothing will be displayed in red. This is working correctly.

Next, it enters this form:
<form method="post" action="process.php"
		onsubmit="return validate(this)"><!-- call this function when the form is submitted if true, the form can be submitted-->
	<tr><td>Forename</td><td><input type="text" maxlength="32"
		name="forename" value="<?php echo $forename?>" /></td></tr>
	<tr><td>Surname</td><td><input type="text" maxlength="32"
		name="surname" value="<?php echo$surname?>" /></td></tr>


When this form can only be submitted if the validate function returns true because of the onsubmit="return validate(this)" attribute. This is what I don't understand I think. If validate(this) returns false will process.php still get called with the data passed to the $_POST array? Otherwise all that will ever happen is the validate(this) being called and the alert box will be shown.

So anyway I'll assume that there is a way it still gets passed to process.php even if there are errors and validate(this) returns false. That would mean the data would go in the $_POST array and be sent to process.php. In process.php all the values such as $forename, $surname, etc will be given values and then passed to their respective validate functions. So fail will either end up being a long string or an empty string. Either way control will be returned to phpHTMLTest.php. And that starts over at the top correct? So when this line is called again:
<tr><td colspan="2">Sorry the following errors were found<br />
	in your form: <p><font color=red size=1><i><?php echo $fail?></i></font></p></td></tr>


$fail will have a value and be printed if there were errors.


Am I seeing this correctly? If so, I can't see how it's not working. Unless something is going wrong with the validate(this) part?

Thanks!


EDIT: Ok stevo, I'll check that out.

This post has been edited by eZACKe: 06 January 2011 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#18 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 04:24 PM

Okay I changed my error system to what you suggested stevo, I just have one question about it:

What is this doing?
function failed($string) 
{ 
$GLOBALS['fail_msg'] = $string; 
}


I never have it being called anywhere.

Lastly, still have the same results. With errors entered all I get is an alert box. No errors in red on my table.
Was This Post Helpful? 0
  • +
  • -

#19 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 04:25 PM

You're logic is flawless following the code. That kind of thinking will be extremely helpful when debugging large-scale programs.

If validate(this) returns false, process.php is never run which is why you only see the Javascript error. All that would be executed is the validate(this) function. The only time the form is submitted to process.php and $_POST updated is if validate returns true.

EDIT: the failed($string) function is actually not used at all. I added it before I thought about what I was doing and realized the error string needed to be appended to, not set which is what the function does. I was being a bit lazy and didn't remove it before posting the code. Sorry to make it more confusing.

I usually add wrapper functions like that whenever using $GLOBALS, because I hate typing the associate array name out. It's a lot quicker for me to type out a function call with a parameter and it allows me to give the function a much more useful and descriptive name than a short associative array variable name could.

EDIT2: We still have the trouble of updating the page. In order to run the proccess.php code even when there are errors you need to change the submit button's functionality to "validate(this); return true;" so that it submits the form and lets process.php run.

EDIT3: Last edit, I swear. If you wanted to get really heavy with the code you could probably just use AJAX to query the process.php with all the given form-data and then output the results of process.php without ever leaving the page or properly submitting the form. That's a bit more advanced though and I doubt you're looking to get into AJAX just yet.

This post has been edited by stevo86: 06 January 2011 - 04:35 PM

Was This Post Helpful? 0
  • +
  • -

#20 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 04:40 PM

So what you're saying is there's no way the red error messages will ever be shown? I understand why, but what I don't understand is how it worked when I had it all in one program, adduser.php. I had essentially the same thing but all of this was in a php echo <<<_END:
<form method="post" action="adduser.php"
		onsubmit="return validate(this)"><!-- call this function when the form is submitted if true, the form can be submitted-->
	<tr><td>Forename</td><td><input type="text" maxlength="32"
		name="forename" value="$forename" /></td></tr>
	<tr><td>Surname</td><td><input type="text" maxlength="32"
.
.
.



When I run that program I never encounter the alert box at all.

I guess my only question besides how that is working is how can I fix/get around that in phpHTMLTest.php?
Was This Post Helpful? 0
  • +
  • -

#21 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 04:57 PM

Well if I can't do both Javascript and PHP erorr handling like this, I guess I just want the PHP.

So I would turn this line:
<form method="post" action="process.php"
		onsubmit="return validate(this)">


Into just:
<form method="post" action="process.php"">



That should then send everything in the $_POST array to process.php. The process.php will use $GLOBALS['fail_msg']. When it has validated all the fields it will be empty or have some text in it. If it's empty then it will print the message. If it's not it should go back to phpHTMLTest.php, should it not? Because that's not what is happening how I have it now. If there's errors my browser goes to localhost/process.php and it's just a blank page. Control is never returned to phpHTMLTest.php. Do I have to send control back to it somehow? If so, I don't know how to do that.

This is the bulk of process.php now:
if(isset($_POST['submit']))// only do this if submit has been pressed, thus error messages will not show before hitting signup for first time
{ 
	$GLOBALS['fail_msg'] .= validate_forename($forename);
    $GLOBALS['fail_msg'] .= validate_surname($surname);
    $GLOBALS['fail_msg'] .= validate_password($password);
    $GLOBALS['fail_msg'] .= validate_age($age);
    $GLOBALS['fail_msg'] .= validate_email($email);
}

if($GLOBALS['fail_msg'] == "" && isset($_POST['submit']))// if there were no errors after signup for the first time
{
	// print out what was entered into all the fields
	echo "<body>Form data successfully validated: $forename, $surname, $username, $password, $age, $email.</body>";
	
	// This is where you would enter posted fields into a database
	
	exit;
}



Thanks!

This post has been edited by eZACKe: 06 January 2011 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#22 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 05:02 PM

My only guess is that because it was defined in the same file and because it wasn't showing the alert box for you, it had to be somehow skipping the validate() function, submitting the form, and populating your $_POST data and error string.

I think you may be better off sticking the process.php code back in the header of phpHTMLTest.php. When you run the form have it's action="validate(this); return true;" This way both the javascript validation is performed AND the PHP validation. Because it's pointed to itself for processing, it should be able to update the error string and display it properly. With the processing being in a separate file I think it's sticking at process.php and not going back to phpHTMLTest.php, where the string would've been outputted. I suppose you could try one more thing before combining them:

// process.php
// After doing the validation tests, if there is an error...
// (assume $GLOBALS['fail_msg'] is set to the proper error string)
if (!empty(get_error())
  header('Location:phpHTMLTest.php'); // redirect to main page.



What this does is if there's any error messages stored in our global it redirects the page back to the form, where you can then display the error message, which would still be set because it's stored in the $GLOBALS array.

In general you really should only be using PHP for backend error-checking. Javascript should be used for validating and letting the user know, via error messages or alert boxes. PHP does error-checking more as a sanity check, but like I said, if you do come across an error message and redirect back to the form page with a $GLOBALS['fail_msg'], you should then be able to output the error string and have them try again.

This post has been edited by stevo86: 06 January 2011 - 05:05 PM

Was This Post Helpful? 1
  • +
  • -

#23 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 05:13 PM

I see what you're saying. I tried putting the process.php code back into phpHTMLTest.php.

I now have this:
<?php
require_once('functions.php');

$forename = $surname = $username = $password = $age = $email = "";

if(isset($_POST['forename']))// check if $_POST['forname'] has been set from a form yet, if so fix it and set it equal to $forename
	$forename = fix_string($_POST['forename']);
if(isset($_POST['surname']))
	$surname = fix_string($_POST['surname']);
if(isset($_POST['username']))
	$username = fix_string($_POST['username']);
if(isset($_POST['password']))
	$password = fix_string($_POST['password']);
if(isset($_POST['age']))
	$age = fix_string($_POST['age']);
if(isset($_POST['email']))
	$email = fix_string($_POST['email']);

if(isset($_POST['submit']))// only do this if submit has been pressed, thus error messages will not show before hitting signup for first time
{ 
	$GLOBALS['fail_msg'] .= validate_forename($forename);
    $GLOBALS['fail_msg'] .= validate_surname($surname);
    $GLOBALS['fail_msg'] .= validate_password($password);
    $GLOBALS['fail_msg'] .= validate_age($age);
    $GLOBALS['fail_msg'] .= validate_email($email);
}

if($GLOBALS['fail_msg'] == "" && isset($_POST['submit']))// if there were no errors after signup for the first time
{
	// print out what was entered into all the fields
	echo "<body>Form data successfully validated: $forename, $surname, $username, $password, $age, $email.</body>";
	
	// This is where you would enter posted fields into a database
	
	exit;
}
?>

<html>
	<head><title>A Quick Test</title>
	<link rel="stylesheet" type="text/css" href="style.css" /></head><body><!--Should handle the CSS-->
	<script type="text/javascript" src="functions.js"></script><!--Should handle the Javascript-->
	
	<table class="signup" border="0" cellpadding="2"
		cellspacing="5" bgcolor="eeeeee">
	<th colspan="2" align="center">Signup Form</th>
	
	<tr><td colspan="2">Sorry the following errors were found<br />
	in your form: <p><font color=red size=1><i><?php echo get_error();?></i></font></p></td></tr>
	
	<form method="post" action="validate(this); return true;">
	<tr><td>Forename</td><td><input type="text" maxlength="32"
		name="forename" value="<?php echo $forename?>" /></td></tr>
	<tr><td>Surname</td><td><input type="text" maxlength="32"



Now the problem I'm getting is when I hit signup it brings me to an error page and says the link appears to be broken.

Should this line be as I have it?:
<form method="post" action="validate(this); return true;">


Was This Post Helpful? 0
  • +
  • -

#24 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 05:19 PM

Not quite. I explained that poorly, I'm sorry. action="phpHTMLTest.php"> <input type="submit" onclick="validate(this); return true;" /> is what I meant.

Try this out.

<?php
require_once('functions.php');

function get_error() { return $GLOBALS['fail_msg']; }

$forename = $surname = $username = $password = $age = $email = "";

if(isset($_POST['forename']))// check if $_POST['forname'] has been set from a form yet, if so fix it and set it equal to $forename
	$forename = fix_string($_POST['forename']);
if(isset($_POST['surname']))
	$surname = fix_string($_POST['surname']);
if(isset($_POST['username']))
	$username = fix_string($_POST['username']);
if(isset($_POST['password']))
	$password = fix_string($_POST['password']);
if(isset($_POST['age']))
	$age = fix_string($_POST['age']);
if(isset($_POST['email']))
	$email = fix_string($_POST['email']);

if(isset($_POST['submit']))// only do this if submit has been pressed, thus error messages will not show before hitting signup for first time
{ 
	$GLOBALS['fail_msg'] .= validate_forename($forename);
    $GLOBALS['fail_msg'] .= validate_surname($surname);
    $GLOBALS['fail_msg'] .= validate_password($password);
    $GLOBALS['fail_msg'] .= validate_age($age);
    $GLOBALS['fail_msg'] .= validate_email($email);
}

if($GLOBALS['fail_msg'] == "" && isset($_POST['submit']))// if there were no errors after signup for the first time
{
	// print out what was entered into all the fields
	echo "<body>Form data successfully validated: $forename, $surname, $username, $password, $age, $email.</body>";
	
	// This is where you would enter posted fields into a database
	
	exit;
}
?>

<html>
	<head><title>A Quick Test</title>
	<link rel="stylesheet" type="text/css" href="style.css" /></head><body><!--Should handle the CSS-->
	<script type="text/javascript" src="functions.js"></script><!--Should handle the Javascript-->
	
	<table class="signup" border="0" cellpadding="2"
		cellspacing="5" bgcolor="eeeeee">
	<th colspan="2" align="center">Signup Form</th>
	
	<tr><td colspan="2">Sorry the following errors were found<br />
	in your form: <p><font color=red size=1><i><?php echo get_error();?></i></font></p></td></tr>
	
	<form method="post" action="test.php">
	<tr><td>Forename</td><td><input type="text" maxlength="32"
		name="forename" value="<?php echo $forename?>" /></td></tr>
	<tr><td>Surname</td><td><input type="text" maxlength="32"
		name="surname" value="<?php echo $surname?>" /></td></tr>
	<tr><td>Username</td><td><input type="text" maxlength="32"
		name="username" value="<?php echo $username?>" /></td></tr>
	<tr><td>Password</td><td><input type="password" maxlength="32"
		name="password" value="<? echo $password?>" /></td></tr>
	<tr><td>Age</td><td><input type="text" maxlength="32"
		name="age" value="<?php echo $age?>" /></td></tr>
	<tr><td>Email</td><td><input type="text" maxlength="32"
		name="email" value="<?php echo $email?>" /></td></tr>
	<tr><td colspan="2" align="center">
		<input type="submit" name ="submit" value="Signup" onclick="validate(this); return true;" /></td></tr></form></table></body>
</html>



Let me know if that works out for you. I just tested it out using your functions.php and that code in a file named "test.php" and it worked out. The javascript alert never showed, but I have to run out for a bit. I'll test that further and see what happens when I'm back.

This post has been edited by stevo86: 06 January 2011 - 05:20 PM

Was This Post Helpful? 1
  • +
  • -

#25 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 05:22 PM

I got it working with this:
<?php
require_once('functions.php');

$forename = $surname = $username = $password = $age = $email = "";

if(isset($_POST['forename']))// check if $_POST['forname'] has been set from a form yet, if so fix it and set it equal to $forename
	$forename = fix_string($_POST['forename']);
if(isset($_POST['surname']))
	$surname = fix_string($_POST['surname']);
if(isset($_POST['username']))
	$username = fix_string($_POST['username']);
if(isset($_POST['password']))
	$password = fix_string($_POST['password']);
if(isset($_POST['age']))
	$age = fix_string($_POST['age']);
if(isset($_POST['email']))
	$email = fix_string($_POST['email']);

if(isset($_POST['submit']))// only do this if submit has been pressed, thus error messages will not show before hitting signup for first time
{ 
	$GLOBALS['fail_msg'] .= validate_forename($forename);
    $GLOBALS['fail_msg'] .= validate_surname($surname);
	$GLOBALS['fail_msg'] .= validate_username($username);
    $GLOBALS['fail_msg'] .= validate_password($password);
    $GLOBALS['fail_msg'] .= validate_age($age);
    $GLOBALS['fail_msg'] .= validate_email($email);
}

if($GLOBALS['fail_msg'] == "" && isset($_POST['submit']))// if there were no errors after signup for the first time
{
	// print out what was entered into all the fields
	echo "<body>Form data successfully validated: $forename, $surname, $username, $password, $age, $email.</body>";
	
	// This is where you would enter posted fields into a database
	
	exit;
}
?>

<html>
	<head><title>A Quick Test</title>
	<link rel="stylesheet" type="text/css" href="style.css" /></head><body><!--Should handle the CSS-->
	<script type="text/javascript" src="functions.js"></script><!--Should handle the Javascript-->
	
	<table class="signup" border="0" cellpadding="2"
		cellspacing="5" bgcolor="eeeeee">
	<th colspan="2" align="center">Signup Form</th>
	
	<tr><td colspan="2">Sorry the following errors were found<br />
	in your form: <p><font color=red size=1><i><?php echo get_error();?></i></font></p></td></tr>
	
	<form method="post" action="phpHTMLTest.php"
		onsubmit="validate(this); return true;">
	<tr><td>Forename</td><td><input type="text" maxlength="32"
		name="forename" value="<?php echo $forename?>" /></td></tr>
	<tr><td>Surname</td><td><input type="text" maxlength="32"
		name="surname" value="<?php echo$surname?>" /></td></tr>
	<tr><td>Username</td><td><input type="text" maxlength="32"
		name="username" value="<?php echo $username?>" /></td></tr>
	<tr><td>Password</td><td><input type="password" maxlength="32"
		name="password" value="<?php echo $password?>" /></td></tr>
	<tr><td>Age</td><td><input type="text" maxlength="32"
		name="age" value="<?php echo $age?>" /></td></tr>
	<tr><td>Email</td><td><input type="text" maxlength="32"
		name="email" value="<?php echo $email?>" /></td></tr>
	<tr><td colspan="2" align="center">
		<input type="submit" name ="submit" value="Signup" /></td></tr></form></table></body>
</html>



Thank you so much for your help! I may have not got everything completely into different programs, but it's definitely a lot cleaner now than when I started.

Again, thanks! :bigsmile:

This post has been edited by eZACKe: 06 January 2011 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#26 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 06:01 PM

Not a problem, sorry for taking so long to actually piece it all together but thats kind of how I work: trial and error. Glad we could get it up and running though and good luck with whatever you're making.
Was This Post Helpful? 0
  • +
  • -

#27 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 06:14 PM

No need to apologize at all. Because of that I got a lot of time to look at how everything works and now definitely understand the flow of a PHP/Javascript/HTML program.
Was This Post Helpful? 0
  • +
  • -

#28 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 06:16 PM

In that case, cheers. Definitely PM me a link to the project you're working on when you start nearing completion, I'd love to check it out if that's possible.
Was This Post Helpful? 0
  • +
  • -

#29 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 06:38 PM

Alright will do. It will must likely be a slow process though since as you can see I'm just learning the basics now :bigsmile:

But I definitely will, I have some cool things planned.

This post has been edited by eZACKe: 06 January 2011 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#30 Cori1995  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-September 12

Re: Making Error Messages Not Display First Time

Posted 06 September 2012 - 12:59 AM

Its Pleasure to understand www.dreamincode.net . The above articles is pretty extraordinary, and I really enjoyed reading your phorum and points that you expressed. I really like to appear back over a typical basis,post a lot more within the topic. Thanks for sharing…keep writing!!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2