I am doing the group assignment that involves using HTML, CSS, JavaScript and PHP. I am using seperated HTML, then send the form data to a seperated PHP file to handle. But the more I learn about PHP, I feel that I can use pure PHP file to contain HTML data too, more convenient.
For example: I have a html file login.html , it has one <form>
element inside the body to login. The form use POST
and it leads to processLogin.php
Should i process them separately or to use single processLogin.php that contain a form and contains the php scripts too?
Mixing languages is not a good idea. Because different languages usually correspond to different layers.
PHP deals with business logic. HTML deals with presentation of the business objects. Thus, you have two layers: the one generates the objects; the other one builds a pseudo-XML representation of those objects.
In the same way, JavaScript deals with interaction, and CSS with the presentation of the content. When you want to change interaction, you don't open an.html file, and you surely don't open a.php file: you open a.js or.coffee file.
Note: extracted form other answer. You can read the full answer here .
Now, my opinion. For a small project u can use together, but if want a robust project try use the MVC pattern . The main aim of MVC Architecture is to separate the Business logic & Application data from the USER interface.
it would be better to separate them, write the html in one file and php in another file, so that will be easy if you want to change something in php code or html, the same for css and js it would be better to write everything separately...
If you plan on taking the “path to recovery”, then this is the first rule that you should take on board. Echo'ing out HTML is horrible and it prevents non-programmers from being able to work with your design, It also prevents your IDE from being able to highlight HTML elements, attributes, ID names and class names!
There are four terms that you need to be aware of:
MVC (Model–View–Controller) frameworks are popular because they help to enforce the separations of concerns principle (they also come with a lot of useful libraries and helper classes). Generally speaking, an MVC framework will split your application up into three separate parts:
Another alternative is to use a template engine such as Twig, which will allow you to separate your logic from your appearance. In a lot of cases, these templating engines are actually used inside some of the MVC frameworks that were listed above! A cool thing about Twig is that is actually supports view inheritance!
If you don't have the time to learn about using an MVC framework or a templating engine, then you should at least organize your application into its relevant parts / folders: Examples:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.