PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev
Чтение книги онлайн.
Читать онлайн книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev страница 9

$_GET []
<html>
<head>
<title> Form Methods
</title>
</head>
<body>
<form method=“get” action="actionpage.php”>
<p> <input type=“text” name=“greeting” size=“15”> </p>
<p> <input type=“text” name=“name” size=“15”> </p>
<p> <input type=“submit” name=“submit” value=“Salutation”> </p>
</form>
</body>
</html>
Save it as a form_methods.php file in the Apache htdocs/post folder created by you.
And now, we’ll create a actionpage.php file for the Output: data transferred from the form.
<?
echo $_GET [‘greeting’];
echo $_GET [‘name’];
echo”! ”;
?>
Save this file in the same directory as the form_methods.php file. This form looks like the following:
Figure 1. A simple HTML form
Let us enter a greeting and a name and click the Salutation button.
You can see that the data sent from a form with the GET method is displayed in the browser’s address bar:
http://localhost/post/formoutputpage.php? <=>
greeting=Hello&name=Emily&submit=Salutation
The Output web page displays Hello Emily!
$_POST []
Now let’s use the POST method instead of the GET method. Edit form_method.php form.
<html>
<head>
<title> Form Methods </title>
</head>
<body>
<form method=“post” action="formoutputpage.php”>
<p> <input type=“text” name=“greeting” size=“15”> </p>
<p> <input type=“text” name=“name” size=“15”> </p>
<p> <input type=“submit” name=“submit” value=“Salutation”> </p>
</form>
</body>
</html>
Edit formoutputpage.php file as follow:
<?
echo $_POST [‘greeting’];
echo “”. $_POST [‘name’];
echo”! ”;
?>
The browser address bar displays formoutputpage.php, but no data transferred using the POST method is visible, so the web page output remains the same:
Hello Emily!
You don’t have to create a second page to read data submitted with form because it is possible to submit the form to the same page. To do this use super global $_SERVER [“PHP_SELF”].
<?php
$self=$_SERVER [“PHP_SELF”];
$greeting=“”;
$name=“”;
If (isset ($_POST [‘greeting’]))
$greeting=$_POST [‘greeting’];
If (isset ($_POST [‘name’]))
$name=$_POST [‘name’];
if (($name!=“”) && ($greeting!=“”))
echo $greeting.””. $name;
?>
<html>
<head>
<title> Form Methods
</title>
</head>
<body>
<?php
print (”<form method=“post” action=“”. $self.””>”);
?>
<p> <input type=“text” name=“greeting”
size=“15”> </p> 21
<p> <input type=“text” name=“name” size=“15”> </p>
<p> <input type=“submit” name=“submit”
value=“Salutation”> </p>
</form>
</body>
</html>
The htmlentities () function
The htmlentities () function is used for security reasons. It converts all characters to HTML entities.
For example, the '<' character will be converted to HTML '<”
If you add the ENT_QUOTES parameter, it will convert double quotes and single quotes to HTML entities.
<?php
$string='<a href="configure-all.com”> Web