Table of Contents
Introduction
When anyone starts development in PHP, the first software they are being told is XAMPP. XAMPP actually stands for cross-platform, Apache, MySQL, PHP and Perl. As the name suggests it is used to run Apache server and hence PHP with it. It is cross-platform and is supported by Windows, Mac, Linux and Ubuntu. In this article we will try to install, run and fix issues related to XAMPP.
How to Install XAMPP
- Visit here and download XAMPP as per your system.
- Once downloaded, install the XAMPP.
- Once installed, you can save your programs here: C:\xampp\htdocs
How to run server on XAMPP
- Once the XAMPP server is installed, you can open it up:
- Here you can start Apache server and MySQL. With this you have started a server that can be used to run your Apps and SQL that can be used to create tables and perform tabular operation:
You can see above that our server is running on port 81. By default is will run at 80.
How to fix Installation error of XAMPP
- In case you get an error that Apache or SQL is not starting.
- Then you need to change the port value, as we have changed from 80 to 81.
Click Config:
- When you will click this file, it will open a notepad, find 80 and change it to any other value:
- Like this you can even change other port values:
But it is recommended to use the default ones, until you get an error while running the server/MySQL.
How to run PHP in XAMPP
To run a php program either the one within HTML content or one with just php programs, we save them as .php files.
We need a server to run php file as it is a server side executable file. We will firstly follow steps mentioned here: “PHP Installation with XAMPP”. Once XAMPP is installed and server is running, then we will save our php files in a folder “test” inside “C:\xampp\htdocs” in windows.
Then we will run http://localhost:/test/ in browser.
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
In case you have saved your above discussed php code in index.php, then it will run something like this:
In case you have a php file without HTML content and you want it to run/executed via frontend (e.g. using AJAX calls), then we will be writing two programs.
The first one is a simple php file “server.php” with given code:
<?php header("Access-Control-Allow-Origin:*, Origin, X-Requested-With, Content-Type, Accept"); // header('Content-Type: application/json'); echo $_POST["method"](); //simple function example function readCall(){ echo 'PHP Call is successful'; } ?>
Here, we have created a function that return “PHP is successful”. In the same folder, we have a html file with given content:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script type="text/javascript"> function script() { // AJAX call $.ajax({ url: "http://localhost:81/test/server.php", type: "POST", data: { method: "readCall" }, success: function(response) { alert(response); }, error: function(request, error) { alert(request); } }); } </script> </head> <body onload="script()"> <h1>HTML Content</h1> </body> </html>
Here, this HTML file firstly loads jQuery from an external link, so that we can perform AJAX call. In second script we have written a simple AJAX call which is calling out our server.php file using its location (http://localhost:81/test/server.php, or simply http://localhost:/test/server.php if you have not set any port in XAMPP). The AJAX gets our PHP echo in response and we have added the response in the alert, which looks like this in browser:
0 Comments