How to connect html to database using php - php

Current scenario:
I want to connect my HTML to database using php, but when I clicked the button from my html form to call the php and save my input to database stored in xampp, it displays the whole code of php file. I tried to place the html together with the php file in htdocs inside the xampp but that doesn't work.
Scenario 2:
I created a separate app just for practice, and that php file contains just a basic input and submit within a form and calls the other php file that contains the code to store my input to database. I opened it in my browser, input my sample data and it worked. I've placed these two files in other location and it doesn't work again.
My question is: what is the difference between the two scenarios?

Something might be wrong with your PHP settings. CHeck these tips.
Make sure that PHP is installed and running correctly. This may sound silly, but you never know. An easy way to check is to run php -v from a command line and see if returns version information or any errors.
Make sure that the PHP module is listed and uncommented inside of your Apache's httpd.conf This should be something like LoadModule php5_module "c:/php/php5apache2_2.dll" in the file. Search for LoadModule php, and make sure that there is no comment (;) in front of it.
Make sure that the http.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This tells Apache to run .php files as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented.
Make sure your file has the .php extension on it, or whichever extension specified in the MIME definition in point #3, otherwise it will not be executed as PHP.
Make sure you are not using short tags in the PHP file (
Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php

Ensure that the php file that runs the php code has the extension .php and not .html
Failing to do so will result in the full output of the php code onto the page.

Related

Require php line of code not working

Very new to all of this and having some issues using a line of code.
All i am trying to do is have my html file have a require statement that pulls a php file however it is not working. Please help.
<?php
require 'localhost/php/loginscreen.php';
?>
"All i am trying to do is have my html file have a require statement that pulls a php file however it is not working".
That to me suggests that you're using an .html file as a base file to pull in an .php file.
Unless you've instructed your server to treat .html files as PHP, you would need to rename that file to a .php extension.
Edit. Consult the following Q&A on Stack on how to do this:
Using .htaccess to make all .html pages to run as .php files?
Your syntax for require 'localhost/php/loginscreen.php'; is incorrect.
You would need to either use a full system path: (as an example, replace that with your system path).
require '/var/usr/public/php/loginscreen.php';
or a relative path:
require 'php/loginscreen.php';
and using http://localhost rather than a possible file:/// url in the browser.
"Strange as there is no error. as i refresh my html file nothing happens."
Had you viewed your HTML source, you would have most likely have seen code, rather than parsed (PHP) directives. That would have been the reason why you did not see any errors, least not php-wise.

When would I want to use .html, vs. .php, as a file extension?

I've noticed that the .html and .php file extensions can be interchanged without apparent effect. Why would I want to use one file extension over the other?
A page ending in .php can include both HTML and/or PHP code (also javascript, css, etc inside their appropriate tags). Note that it is perfectly fine for a page without any PHP code to still have the .php extension.
However, if your page does include PHP code, the filename extension must be .php. Try it - on most web servers this won't work:
FILENAME: test.html
<?php
echo 'Hello there';
The above page will be blank. But if you rename it to test.php, you will see the hello message.
Filename extensions are also an indicator to yourself, or other programmers, as to what type of code the file contains. It is clear at a glance that a file ending in .HTML does not contain any PHP code (especially since any PHP code contained within won't work unless the webserver config is specifically modified to allow it).
One Final Note: these days it is pleasing to have web pages that do not end with an extension at all. Of course, you cannot leave off the extension of a .php or .html page... but you can hide the extension (including the period), making the page look like it was served by Flask or React or etc. You do this via a .htaccess file (yes, exactly like that, dot and all) that sits in the root folder of your website (same folder as the index.php or index.html). See:
https://code-boxx.com/hide-php-extension-url-htaccess/
https://tecadmin.net/remove-file-extension-from-url-using-htaccess/
Here is an interesting tool to help build .htaccess files
Use .html as a default.
If your page is running phpscripts then use .php
So, if you are communicating with server, use .php
.html and .php are file extensions but the more important question is how they are run.
A .php file can run server side script and take in mysql queries and open a connection etc...all of which are server-side functions.
Html is static and only displays static content but that has now changed with HTML 5.I suggest you do a simple search to learn more about php and html and their fundamental differences.
Files are handled depending on config and context. Shebangs, default programs, Apache Handler's, HTTP Headers, etc. describe handling files in various scenarios.
Executing Files In Terminal
The .php extension indicates that it is a PHP script, but the extension isn't necessary.
example-file.php
<?php
echo 'Hello World';
The script can be executed with PHP, which is clear because of the extension:
> php example-file.php
example2-file
#!/usr/bin/env php
<?php
echo 'Hello World';
With a shebang on the first line the OS can try to use the correct interpreter for the user so that the command is simplified to:
> ./example2-file
Some of the implementation details are hidden from the user by removing the file extension.
Packages often retain the extension on the source, but drop the extension during installation.
Default Programs
An extension can indicate to an OS which program to use to open a file.
Files ending in .php on my computer open in an IDE for editing whereas .html files open in a browser.
Servers and Headers
Web servers can send a file with any extension and content-type since many files don't actually exist, but are dynamically generated.
PHP web servers will serve .php files with the text/html content-type because the PHP is interpreted into text. Servers configured to return the raw PHP file as another content-type, i.e. servers not configured for PHP, will cause the web browser to download the source file rather than view the rendered file as HTML.
Since the resulting file after execution is HTML and web servers can dictate the extension, some developers decide to use .html in the URL and have them correlate to .php files to execute and return. Or the URL can not use an extension at all.
Using distinct extensions has the same purpose in PHP as it does in any language -- it makes it easier to determine the type of file you're using.
You may want to ease your web server's burden by having .html files not ran through the PHP processor, or you may want to have your PHP files not labeled .php to help hide what technology you're using server-side.

Is it possible to make file name with .html extension where php codes are included in the file?

I want to that if it is necessary to make files with .php extension or it is possible to add php codes into a file wiht .html extension.
Yes, at the following to the .htaccess file (assuming you're using Apache)
AddHandler x-httpd-php5 .php .htm .html
More Info.
Take note that this is not efficient server-wise, as PHP will be fired for all pages in your script (you lose the ability to have high-efficiency static HTML web-pages, which are sometimes needed.
If you want to keep your HTML extension of the file AND you want to use PHP code within the file, you MUST tell the webserver to do so by adding the .html extension and tell the webserver to let PHP handle it.
However, this is hugely inefficient, because PHP will be fired for each HTML request.

Can you add a .php file to your .html file?

I have a website made and i have saved the file as .html, I now want to add a .php file. Is this possible as i have tried so hard to do this. Id appreciate any help given :)
You can do several things.
Rename your .html files to .php.
Tell your web server to pass .html files to the PHP interpreter. In Apache you'd do this by putting AddType application/x-httpd-php .html .htm in your .htaccess file or your Apache configuration.
If you have installed PHP, you can rename your file to *.php and insert PHP codes!
You want to put PHP code inside a .html file? You'd have to configure the webserver to treat the .html file as a PHP script. Technically, there's no such thing as a PHP script. There's only files which have at least one <?php ?> code block within them. As such, ANY file can contain PHP code. The problem is getting that file into the PHP environment.
Unless your webserver is told to hand the .html file over to the PHP interpreter, you cannot execute the PHP code contained within it - the raw code will get sent out as part of the html page.
Just rename your .html file to .php, if your webhost supports it.
If you run your own server you can even configure PHP to also run .html files, but it's not recommended because of the performance penalty on all html files.
Yes, you can include php file into html file. i m sure that this link will help you.
http://php.about.com/od/advancedphp/p/html_php.htm

Can't run php code from inside html file

I am having a problem running my php code from inside an html file. I have tried the .htaccess method and the handler method. I'm using a webserver with cpanel(version 11), apache(2.2.15) and php(5.2.13).I have also tried the files on xampp and they work perfect, but when I upload them to the webserver the php code from inside the html will not run.
Any suggestions, please?
You need to inform Apache that files of type 'html' need to be parsed by PHP. You can do this by adding this to the Apache configuration file (httpd.conf):
AddType application/x-httpd-php .html
However if you have other HTML files I would advise just rename the file to PHP (or set up some internal mapping so you don't specify an extension all together). If you have static HTML files that you serve, it's redundant to have them being executed by the PHP interpreter.
Why don't make it a PHP file and then rewrite PHP extension with HTML in .htaccess?

Categories