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 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.

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:

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.

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?

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;
}

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.

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.

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.

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!!!