Validation Doesn't Work

Posted 21 July 2011 - 08:42 PM

Hello again, I was working on my contact form validation and was trying to set the page up so that if there is missing data then it would load an identical page with a message that says what is missing above the contact form. The code below is the identical page with the validation. Right now it takes you to the success page even if there is missing information. Could you please look it over and tell me what I'm doing wrong?

Re: Validation Doesn't Work

Posted 21 July 2011 - 10:24 PM

It's messed up at the moment and contains no PHP which is why I didn't include it. What I was trying to do was show the contact form and tell the user what information they still need to fill in while still keeping their responses so they don't have to fill it out again.

Re: Validation Doesn't Work

Posted 22 July 2011 - 06:01 AM

I'm not sure I see a problem, but a few notes if I may

1. Having two copies of your form is a bad idea. You will inevitably make changes to one that you forget to reflect in the other. You could take the above code and, with a few changes, make it the only place that has the actual form. Your choice, ultimately, but code duplication is bad bad bad.
2. When you pull out your post vars, you should probably trim the inputs. Otherwise, if the person hits space or something, it'll register as a valid input.

$name = trim($_POST['name']);

That may fix your problem, or maybe not.
3. Instead of empty, you could use strlen.

if (strlen($name) == 0

I'm not saying that empty is wrong... it should work. But this is a different path just in case variables coming out of the $_POST superglobal are created funny.

Re: Validation Doesn't Work

You should refactor your code into something more concise and understandable such as the following. It will consolidate your code and make it much easier to understand.

This page can be used as your default contact form in all instances, so just link straight to this page and it will show the form with no errors (unlike your old one..) on first pageload, then do error checking only on subsequent requests. There is no way that this could redirect without the form being correct, oh and I added checking for a valid email.

Just a mini disclaimer, this isn't properly tested per se (there's no errors but still untested!), but it should be easy enough to figure out if anything's wrong.

EDIT: Also on another note, you shouldn't be doing things like <li>&nbsp;&nbsp;&nbsp;&nbsp;</li> or even [li]<br/> <br/> <br/> <br/>[/li], there's no sense in that. Instead try to work your page's layout out completely in HTML.

As another aside, your errors should just be text, and then proceed to output them in the foreach loop as individual <li> elements in a <ul>. It all helps to producing more semantic, readable code that makes sense.

Re: Validation Doesn't Work

Posted 23 July 2011 - 09:35 AM

Sorry for my late reply, I've got a weird work schedule this week.

Quote

Having two copies of your form is a bad idea.

The reason I'm having 2 copies of my form is because I am following the Head First PHP & MySQL book as I work on this website. To me it seems like a good idea if I can just get it so that when it gives the error it also displays the user's old message so they don't have to type it again.

I don't know what was wrong the other day, but right now the validation works and it displays the error message it just doesn't keep the user's information that they entered.

Re: Validation Doesn't Work

Posted 23 July 2011 - 10:10 AM

Pleeeeease stop following that book.

If it's teaching you bad practice like this, especially in the early stages, it's a terrible terrible! book.

The way that I demonstrated to do it would be a perfectly understandable and much better way to structure your code, I also explained a few key reasons as to why it's better than your current code which should help you to understand why I did it like that.

To achieve your goal of having it store the form values you certainly do not need a second form, this is essentially duplicating code and is again, bad practice. You can easily use the code that I am demonstrating below to have everything contained within a single page, and with a slight bit of extension, you can even put the contents of success.phtml in there.

The code below will define default values for each of your form elements (ie. blank when it's not posted) and populate them if they exist.

EDIT: Another reason to do it this way rather than the initial way, what happens when a user visits the submission page without POSTing any data? It will show some nasty errors to the user which is never expected nor good.

Re: Validation Doesn't Work

Pleeeeease stop following that book.If it's teaching you bad practice like this, especially in the early stages, it's a terrible terrible! book.

CTphpnwb, on 23 July 2011 - 01:26 PM, said:

Yes, that book is terrible.

The next few pages teach a better way of doing this. Basically the book teaches a right way and a wrong way to go about doing all this. So please next time, don't tell someone to stop reading the book if you don't know how the book actually teaches. If I had followed your advice then I would have spent another $50 for a book when I really don't need to.

I'll work on revising my code when I get the chance and will post back if I need further help with this issue.

Can you demonstrate what the "good" method is? I have seen no statement made saying that it's bad.

Nowhere does it suggest that you should check if the form is posted before using the $_POST variables, it just uses them. I find it quite funny that in the first few pages it says "Assume everyone is out to get you", and then they do this? Anyone can spam an email to you with this code. Actually I just read through the security section aswell (well, scanned through!) and all it really teaches you is how to use basic authentication and ignore values that aren't approved by an admin.

It does not teach parameterized queries or anything that you'll need for the real world