Category: PHP

How to use posted data in the same page

GOAL

To post the input data and display it on the same page with PHP.

Environment

Windows10
XAMPP 7.4.10

Preparation

1. Change the PHP program

Create a new PHP file “index.php” to submit data. You can see the details of this program in “How To Run PHP in XAMPP“.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>test</title>
</head>
<body>
	<form action="posted.php" method="POST">
		<label>Input message:</label>
		<input type="text" name="message" /> <input type="submit" value="Submit!" />
	</form>
</body>
</html>

2. Set action = “”

The action specifies the file to call. If you won’t move the page, action should be “”.

Method

Method 1. Use $_SERVER[‘REQUEST_METHOD’]

$_SERVER[‘REQUEST_METHOD’] is the variable to store the requested method. It returns the called requested method when the page is requested.

<body>
	<?php
	if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    	echo "The input data is ".$message;
    }
    ?>
	<form action="" method="POST">
		<label>Input message:</label>
		<input type="text" name="message" /> <input type="submit" value="Submit!" />
	</form>
</body>

Method 2. Use $_POST directly

<body>
	<?php
	if ($_POST) {
    	echo "The input data is ".$_POST['message'];
    }
    ?>
	<form action="" method="POST">
		<label>Input message:</label>
		<input type="text" name="message" /> <input type="submit" value="Submit!" />
	</form>
</body>

The result is the same as above.

Method 3. Use isset($_POST[‘message’])

<body>
	<?php
	if ($_POST) {
    	echo "The input data is ".$_POST['message'];
    }
    ?>
	<form action="" method="POST">
		<label>Input message:</label>
		<input type="text" name="message" /> <input type="submit" value="Submit!" />
	</form>
</body>

The result is the same as above. The strong point of this method is that the existence of each keys of $_POST can be determined.

Access MySQL with PHP in XAMPP

GOAL

Apply to access MariaDB(MySQL) with PHP in XAMPP

Environment

Windows10
XAMPP 7.4.10

At first, install XAMPP (“Installation of XAMPP“) and If you’ll use MariaDB in a production environment, make it secure (“How To Set MySQL Password in XAMPP“). And I create database “photo_sharing” according to “How to Create Database of MariaDB in XAMPP“.

About PDO

PDO, PHP Data Objects, is an extension to access database in PHP.

Reference: PHP Data Objects in PHP manual

(more…)

How To Run PHP in XAMPP

I installed XAMPP in Installation of XAMPP. Today, I’ll use XAMPP as a test environment for PHP.

GOAL

To setup XAMPP and start PHP development in XAMPP.

How to start XAMPP

Be sure that the started module is stopped before quitting the control panel.

Method 1 Use xampp/xampp-control.exe

Right-click and run as administrator “xampp/xampp-control.exe” or “XAMPP Control Panel” in start menu.

Click “Start” buttons on the control panel.

If you add modules as a service, the check box is checked.
*You can’t see whether the check box is checked if you run the control panel as a non-administrator.

In this article below, a situation in which all modules are not registered as a service is supposed.

Click “Start” to start Apache and access localhost to confirm that apache is running.

Open browser and input “localhost”
The index page of XAMPP

Method 2 Use batch file

Use batch files in xampp\ directory to start each application individually.

xampp\xampp_start.exe
xampp\xampp_stop.exe
xampp\apache_start.bat
xampp\apache_stop.bat
mysql_start.bat, mercury_start.bat and filezilla_start.bat

Run your php program

The directory “xampp\htdocs” is document root that is assigned to localhost. So you can run your php program by putting the php file here.

Open xampp\htdocs\index.php and you can see the program to redirect users to localhost/dashboard by using header() function.

<?php
	if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
		$uri = 'https://';
	} else {
		$uri = 'http://';
	}
	$uri .= $_SERVER['HTTP_HOST'];
	header('Location: '.$uri.'/dashboard/');
	exit;
?>

1. Create “test” directory in xampp\htdocs.

2. Create PHP files

Create 2 PHP files in the “test” directory, index.php to post input data and posted.php to get and display the input data.

xampp\htdocs\test\index.php

Use POST method to send the message.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>test</title>
</head>
<body>
	<form action="posted.php" method="POST">
		<label>Input message:</label>
		<input type="text" name="message" /> <input type="submit" value="Submit!" />
	</form>
</body>
</html>

xampp\htdocs\test\posted.php

Get the posted data by using $_POST[<name>].

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>posted</title>
</head>
<body>
	<p>Message "<?php echo $_POST["message"] ?> " is posted!</p>
</form>
</body>
</html>

3. Change the redirect destination URL in xampp\htdocs\index.php

xampp\htdocs\index.php

<?php
	if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
		$uri = 'https://';
	} else {
		$uri = 'http://';
	}
	$uri .= $_SERVER['HTTP_HOST'];
	header('Location: '.$uri.'/test/');  // Change here!
	exit;
?>

4. Access localhost

Access localhost and the page is redirected to localhost/test/. Input any message you like and press submit button.

The posted page will be displayed and you can see the message you input.

Installation of XAMPP

Let’s develop your site with XAMPP.

GOAL

Today’s goal is to understand what XAMPP is and how to install it. This article doesn’t contain how to use XAMPP or PHP.

Environment

Windows10
XAMPP 7.4.10

What is XAMPP?

XAMPP is a free development environment for PHP. It contains the set of basic free software for web development, such as Apache, MariaDB as a SQL server, PHP, Perl, phpMyAdmin and OpenSSL.

You can see what software is included in XAMPP in the official site.

XAMPP is an acronym for cross platform(X), Apache, MariaDB, PHP and Perl.

How to Install XAMPP

1. Download

Access “Download” page in the official site. And download any version of XAMPP you like.

(more…)