PDA

View Full Version : Basic PHP Tutorial - 7



ctt1010
11-01-2014, 02:17 PM
In this tutorial, we will learn how to integrate PHP into HTML. For example, we will use a HTML textbox and a button to set a PHP variable. Exciting!

We will use the $_POST["XX"]; in order to gather information from HTML.



<?php
$fname= $_POST["fname"];
$lname= $_POST["lname"];
?>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" size="12" maxlength="12" name="fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="lname"><br /></form>
<?php
echo "Hello, ". $fname . " " . $lname .";
?>


This looks a bit complicated but it isn't. In the first PHP section we are defining the two variables to be what the HTML will send as the First Name and the Last Name.

The HTML section creates a form with the "post" method, which comes from $_POST. Then the name is submitted which is what it sends the post as eg. $_POST["fname"];
In the second section, we are using the $PHP_SELF super global, which allows us to use the value of the fields, specified under it, in the same file. Usually, for such forms two files are created - the first one is the HTML form itself and the second one is the backend PHP file, which does all the work.

The second PHP section prints out the variable once the form is submitted.

Jrboss321
09-29-2018, 12:50 AM
Oh good to know