Jump to content
Killersites Community
Sign in to follow this  
antpower26

.php with html 500 error

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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 by khanahk

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×