Making Error Messages Not Display First Time

Posted 05 January 2011 - 09:54 PM

I'm very new to PHP and I'm working on a simple form validation using PHP and Javascript. Everything seems to be working fine in my code except that the first time the page is loaded my error messages saying that I have left a field blank or whatever are shown. Now obviously they are going to be empty the first time because you haven't given the user a chance to enter anything.

I know why this is happening and even on what line of code it is, but I can't come up with a way around it.

Replies To: Making Error Messages Not Display First Time

Re: Making Error Messages Not Display First Time

Posted 05 January 2011 - 10:01 PM

In situations like this, I usually write a function that reads the $_POST array to make sure that all variables are set and the values are not empty. This can be done with a simple for loop ($_POST is an associative array). If this function returns false, I leave a message and call exit.

Something is going very wrong now though. Now when I hit signup the page gets refreshed but no error messages happen at all. Even if I fill in the form correctly or with errors nothing happens. How did these little changes completely screw it up?

EDIT: Ahh, just figured it out. Forgot I had to add a name to the submit button in order to access it from the $_POST array.

Re: Making Error Messages Not Display First Time

Posted 05 January 2011 - 10:42 PM

I may be mistaken but I think you just need to add an </head> tag to display the results. They should be showing in the source either way, if you hit view-source they should show up somewhere in the <head> section. If not, try making your new code like this:

Re: Making Error Messages Not Display First Time

Posted 05 January 2011 - 11:07 PM

Good to know its working, but the issue you might haven't noticed here is the way its written isn't so good. You have mixed html, css, javascript and php code altogether, which is a bad practice. If you need to make a simple change to a logic, you have to go through all the code, similarly any alteration to presentation will need take care of logical part to ensure nothing has broken and its still working. So try to organize the code which will be very helpful in the long run.

Re: Making Error Messages Not Display First Time

Posted 06 January 2011 - 07:22 AM

atik97, on 06 January 2011 - 02:07 AM, said:

Good to know its working, but the issue you might haven't noticed here is the way its written isn't so good. You have mixed html, css, javascript and php code altogether, which is a bad practice. If you need to make a simple change to a logic, you have to go through all the code, similarly any alteration to presentation will need take care of logical part to ensure nothing has broken and its still working. So try to organize the code which will be very helpful in the long run.

How exactly do you suggest doing that? Like I said I just started learning PHP and Javascript and I'm only half way through a beginners book. I'm guessing this book most likely wont get into "good practice".

So now the HTML code is pretty much, just HTML. If you want to update your content it only takes a few minutes because the only thing in your HTML file is a few lines of markup mixed with straight content.

Then in your CSS file, you've got all the space in the world to style away however you'd like and you know exactly where to look to adjust the visual properties of your elements.

Also, thanks to the <script src=""></script> part, your Javascript functions are all tucked away in their own file so if you decide to modify them, you know exactly where to go and you get straight to the Javascript, as opposed to scrolling through 300 lines of content/HTML that would otherwise hide the Javascript and make it harder to debug.

Lastly, the require_once() PHP function keeps all your PHP neatly tucked away in it's own hiding place and the process.php file is also kept separate so when you want to adjust how the form is processed, the code that does the processing is nicely isolated and easy to read through and modify.

All very simple tricks that make your life easier in the long run. I definitely agree and suggest you take a whack at it at the very least. It may not seem like a big deal on smaller projects, and may even seem counter-productive, but when your projects start getting larger and the code gets longer, it's EXTREMELY nice to be able to have one file for PHP functions, one for Javascript, one for CSS, and one for HTML/content. And even breaking it down further so you have one for PHP form validation functions, one for PHP database functions, etc. Same with the Javascript files. It's all about organization and it really does save time and headaches down the road.

Re: Making Error Messages Not Display First Time

A quick question for you stevo: The basic form document you've posted, is that a .html document? I wasn't aware that you could put php directly into html without it being a .php file?

No, I meant it to be a .php file. I only use .php files as of late. Though I'm fairly certain you can use a .htaccess rewrite rule to make them all appear as .html files or to force the PHP interpreter to process .html files. I can't remember which way it worked, but it is possible to end up with index.html or whatever.html even if you do have PHP code in there, and it will get processed.

Re: Making Error Messages Not Display First Time

Okay I think I might be getting a little lost in trying to separate these into a more organized program.

I've turned the one long program into 5 smaller ones:

phpHTMLTest.php -- Used for displaying a form and a table

style.css -- Used for my CSS code

functions.php -- All of my PHP functions are in there

functions.js -- All of my Javascript functions are in there

process.php -- All of my code that handles processing the form is in there

phpHTMLTest.php is my main page or I guess you'd call my "index".

Many things do wrong when I run this:

When I hit submit only the Javascript code seems to be running. An alert message comes up, which isn't really suppose to come up. It didn't when I had it all in the one program called adduser.php from my original post. The $fail message is not being displayed at all.

I put a debugging echo statement at the top of process.php and it gets called as soon as the phpHTMLTest.php is run. It doesn't even wait for the signup button to be pressed. It doesn't seem to get called at all after pressing signup though if errors are found when validating (I think the validation that occurs is the Javascript validation, not the PHP because as far as I can tell process.php is not called).

<?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
{
$fail = validate_forename($forename);// call function to validate the forename from $_POST. If it's validated return empty string, if not return error message
$fail .= validate_surname($surname);// add on to the error message if one exists...
$fail .= validate_username($username);
$fail .= validate_password($password);
$fail .= validate_age($age);
$fail .= validate_email($email);
}
if($fail == "" && 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;
}
?>

EDIT: Been working on it for a while now. Fixed almost all of my problems, but still have a couple which are the ones that remain in the list above.

Re: Making Error Messages Not Display First Time

Sorry to leave you hanging, I finally crashed after just about 62 hours of no sleep.

Firstly, process.php doesn't need to be a require_once(), you only need to put that in the action="process.php" part.

Secondly, and this may or may not even be causing any of your problems, but I would add semi-colons to the end of your statements in "functions.js". It makes things a bit clearer to read through and a bit more uniform since you have to do it for PHP anyways.

Third, one small discrepancy I noticed is that in your PHP version of validate_password you have a function that verifies that at least one lower case, one upper case or one number were used, however in the javascript you required at least one lower case, one upper case, AND one number. You used || in the PHP and && in Javascript basically, which will result in different actions.