antpower26 Posted October 16, 2012 Report Posted October 16, 2012 Hi, I've studied PHP and am now building a project with a mySql database, PHP, SQL, PDo, Ajax etc....... This is a really basic and practical question, I have PHP code in my registration form page, firstly this page was HTML, but since adding the PHP for the form validation, I've figured out that the best idea is to change the filetype to .php, I'm not sure whether it is a good idea to embed php in a .html file? However sending all this across to the server when I type in the url on the testdomain I get a 500 error. So what are the best practices, if I want to have php and html co exist, say for form validation, or dynamically changing the html based on something changing in the database and outputting that. Should the whole site be .php, or can you have some html pages linking together with .php that also outputs html. If all pages are .php, because the whole site is working with the server side, is there some configuration setting (ht access?) that needs to be changed in order for the browser to render the html, and consequently the css, jQuery etc........ I know PHP the language well, and I can write programs with it, its just getting all of it working in a browser application:))))))) Thanks Ant Quote
Mick Posted October 16, 2012 Report Posted October 16, 2012 So you've been writing stand-alone PHP programs? It's a good way to learn a language, and it seems like you've come a long way in knowledge, so kudos to you. I like the Killersites approach better, where early on you have practical web programming instruction-- integration-- such as PHP embedded in HTML. In just the 2nd course, "More PHP", I completed a well-taught project using PHP, HTML, Javascript, and jQuery to display posts from an RSS feed onto a web page. To answer your question, yes, you would change the file type. So, instead of the HTML file being index.html, for instance, it would be index.php. This is done often. The browser will still interpret the HTML (via the html tag), CSS, and Javascript (by default in HTML5, with type= in earlier versions), and jQuery with the src library included in the (javascript) script type tag. I don't know if it a "best practice," but it is a very common one. I don't have enough knowledge to know what alternatives there are (or why you would want to use them). Perhaps someone else can answer those questions. Quote
falkencreative Posted October 17, 2012 Report Posted October 17, 2012 I'm not sure whether it is a good idea to embed php in a .html file? Yes, that's pretty standard practice. A .php file doesn't always contain just PHP code. So what are the best practices, if I want to have php and html co exist, say for form validation, or dynamically changing the html based on something changing in the database and outputting that. Should the whole site be .php, or can you have some html pages linking together with .php that also outputs html. If all pages are .php, because the whole site is working with the server side, is there some configuration setting (ht access?) that needs to be changed in order for the browser to render the html, and consequently the css, jQuery etc A .php file doesn't necessarily contain just .php code. It can also contain HTML, CSS, Javascript, etc, and non PHP code will be displayed by the browser just fine. Basically, just use the .php file ending for whichever files run PHP code. However sending all this across to the server when I type in the url on the testdomain I get a 500 error. Hard to say what is going on without more details. I imagine it is some sort of issue regarding the way your server is set up. Can you view regular .html files on the server? Quote
khanahk Posted October 17, 2012 Report Posted October 17, 2012 (edited) 500 error usually occurs because you have an .htaccess file that has improper 'Directives', from my experience. Try removing the .htaccess file from your root directory and see what happens. This file is often hidden and in many FTP clients you will need to change an option to view hidden files. Or, make sure you are using a .php extension for all files that contain PHP code. Most servers don't read PHP code in a .html file by default although you can configure them like that. Edited October 17, 2012 by khanahk Quote
Mick Posted October 18, 2012 Report Posted October 18, 2012 This is a modification of my previous reply. As a general rule, you want to keep your PHP code separate from your HTML code as much as possible. The reason is so that a web designer can do what she/he wants with the HTML and CSS without messing up your code. The PHP code is included in the HTML file via an include statement in the head section. In the PHP videos, the PHP and MySQL course, Stefan starts by mixing PHP and HTML in the same file, and ends by separating the two as much as reasonable. Here is a sample resulting HTML file: <!Doctype HTML> <html> <head> <title>Use PHP to Display mySQL Database Table Records</title> <meta charset="utf-8"> <?php include("database-connect-inc.php"); ?> </head> <body> <h1>Use PHP to Display mySQL Database Table Records</h1> <h2>Records Follow:</h2> <p> <?php echo $my_rows; ?> </p> </body> </html> Quote
antpower26 Posted October 18, 2012 Author Report Posted October 18, 2012 Hi guys thanks for all your help, the 500 error occurs when there is a fatal error in the PHP, the server stops reading the code. To figure that out you can go to the error logs in the cpanel with your hosting provider. Like firebug but for php....... Anyway that's what I learnt yesterday:))))) Ant Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.