Validation Doesn't Work

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 1415 Views - Last Post: 25 July 2011 - 03:44 PM Rate Topic: -----

#16 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,614
  • Joined: 08-August 08

Re: Validation Doesn't Work

Posted 24 July 2011 - 04:31 AM

Teaching people to mix languages is never good, and teaching a beginner to do it is always bad. First it creates the false impression that the languages are run together when they're not even run on the same machine or at the same time. Then it creates code that is difficult to read, and that's something that beginners certainly don't need holding them back. Finally, the PHP code is highly repetitive. Lines like this are not only wasteful, but they add to the confusion:
    $name = $_POST['name'];

While I haven't read the book I can say that unless the style and content shown in that code is an aberration mixed in with otherwise stellar examples, it is a terrible book.

This post has been edited by CTphpnwb: 24 July 2011 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#17 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 24 July 2011 - 07:48 AM

Then what book do you recommend?

I guess it's ok to ask that. I didn't see a topic that was sticky'd to the PHP forum for this question.
Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,614
  • Joined: 08-August 08

Re: Validation Doesn't Work

Posted 24 July 2011 - 08:05 AM

I'm sure others will have books to recommend, but my suggestion is to learn another programming language before PHP. One that is strongly typed, like C or C++. The reason for that is that in those languages you will rarely mix other languages. You could mix machine code, but that isn't going to be in a beginner book. That forces you to think of only the language you're using, and you need to follow strict (by PHP standards) rules for writing your code.

PHP on the other hand is much trickier because it will let you toss in HTML, Javascript, and CSS without so much as a warning. It doesn't care if you use functions and classes, or if you just write a series of instructions. It's this deceptively easy entry into the language that seems to really mess up new PHP coders who don't come to the language from another one. If you haven't learned another language before coming to PHP you're very likely to end up with spaghetti code.
Was This Post Helpful? 1
  • +
  • -

#19 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 24 July 2011 - 08:14 AM

I've used C++ before, and I have experience in Visual Basic, C#, and Java.
Was This Post Helpful? 0
  • +
  • -

#20 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 24 July 2011 - 08:18 AM

In that case you really shouldn't need a book to learn PHP programming.

There is a complete reference available at php.net, and once you've learned the very basics (C-style syntax, superglobals, etc) you can pick up the rest on your own.

Most functions have full examples when you view the documentation that explains different ways of doing things and will help you to learn even more just by using 1 new function.

Now most of this is just common programming sense, especially when it comes to security in your applications and queries, and how to layout your code. If you really do have a lot of experience with other languages you should know that it's always better to separate your application logic from your display, thus mixing PHP / HTML is a bad idea, yet it was still done. There are of course exceptions to this rule, for example if you were to use PHP as your templating language aswell, so to speak, but even then you should be putting all of your logic into separate files.

As a simple rule of thumb using echo to output HTML inline from within other HTML is bad, when you know the basics such as this and general programming basics, you should be able to see that examples such as that form displayed in the book is bad practice.
Was This Post Helpful? 1
  • +
  • -

#21 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,614
  • Joined: 08-August 08

Re: Validation Doesn't Work

Posted 24 July 2011 - 08:54 AM

As RudiVisser notes, you should be putting all of your logic into separate files. Apply your knowledge of C++, C#, and Java to the code in that book. What does PHP consider the HTML to be? Answer: It's just random text. Would you throw in a bunch of text in the middle of your C code?

By the way, check out my separation tutorial. If you know MVC from your experience with other languages then use that along with the methods I show for keeping HTML out of PHP code.
Was This Post Helpful? 0
  • +
  • -

#22 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 24 July 2011 - 05:48 PM

Any advice for books? I'm more of a hands-on learner mixed with an audio-visual. So if I had video tutorials to go by as well as an actual book to hold then I'd remember everything a lot better!
Was This Post Helpful? 0
  • +
  • -

#23 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:19 PM

View PostRudiVisser, on 24 July 2011 - 10:18 AM, said:

Now most of this is just common programming sense, especially when it comes to security in your applications and queries, and how to layout your code.


Maybe I'm misunderstanding something, but even in this tutorial they mix HTML and PHP.
Was This Post Helpful? 0
  • +
  • -

#24 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:23 PM

That tutorial has been around for a long time.

PHP was once known as a "templating" language (and still, to an extent, is). In this example, it's doing a simple echo, however there is no presentation logic in there at all, it's simply echoing something out.

Did you look at my example of a way to do your form? In there there are also echo's of the li elements, but no presentation logic or anything is in that code at all apart from a loop and an echo statement.
Was This Post Helpful? 0
  • +
  • -

#25 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:36 PM

View PostRudiVisser, on 25 July 2011 - 02:23 PM, said:

Did you look at my example of a way to do your form?


I did but I didn't pay close attention to it because I don't like to be handed code. I like to figure things out with my own but with some guidance.
Was This Post Helpful? 0
  • +
  • -

#26 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:41 PM

View PostRandomlyKnighted, on 25 July 2011 - 08:36 PM, said:

I did but I didn't pay close attention to it because I don't like to be handed code. I like to figure things out with my own but with some guidance.

Take a quick look at it and you'll see how it's separated, it could (and should) be further separated, but at least this doesn't put presentation logic into the main HTML.

You could also take from it how all of the main code is grouped together so nothing really gets in the way of anything else, and it all makes sense.

This post has been edited by RudiVisser: 25 July 2011 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#27 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:49 PM

Ok, I noticed that all your PHP is at the beginning except for a small script which is in the middle of your HTML. Why do you have that script there? Couldn't you put it with the rest of the PHP logic?
Was This Post Helpful? 0
  • +
  • -

#28 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:51 PM

That's what I'm trying to say, what's down in the HTML is not logic. It's simply outputting the variables that were set at the top.

It's perfectly valid to output variables via PHP into code, and use simple templating logic such as loops. You should just remember that your template should only ever be outputting variables that you have set previously (think of an MVC architecture where the Controller sets variables for the View).

This post has been edited by RudiVisser: 25 July 2011 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#29 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:55 PM

While I do have experience with the other languages I'm not really familiar with the MVC architecture.

Ok, so I just reviewed the script that is outputting the variables and it makes more sense now. One thing I noticed is that this page is called contact.php and when you hit submit the form calls itself.
Was This Post Helpful? 0
  • +
  • -

#30 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Validation Doesn't Work

Posted 25 July 2011 - 12:59 PM

Yes exactly, I said this earlier that you don't need a separate page for just the form and just the processing.

When you first hit that page, nothing will happen at all, as it checks for the existence of the $_POST['submit'] variable, if that isn't set nothing will happen, and further down in the code $errors would not be set, so nothing would be output (unlike your first code).

When you submit the form with invalid data, $_POST['submit'] will be existant, and as such it will check the form values that you posted through to see if they're OK, otherwise set an error message into an array. Down in the HTML, it will check if there are any $errors, which there are now, and loop over/output them.

When you submit it with correct data, $errors will be empty and the form will be emailed off to you, and you will subsequently be redirected.

Does that make sense? :)
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3