HTML and PHP code On a Single Page
In one example we saw that using PHP you can respond or process the data to be submitted by HTML form i.e. information will be gathered by HTML form and further processed by PHP. We will do the same thing but on single page only.
We will HTML and PHP code On a Single Page, and $_SERVER[“PHP_SELF”] a superglobal variable, which returns filename of currently executing script. This returns the path along with file name and this is used with action attribute of FORM tag.
Example of HTML and PHP code On a Single Page :
echo $_SERVER['PHP_SELF'];
Example : In form tag
<form name=”frm1″ action=”<?php echo $_SERVER[“PHP_SELF”]; ?>” method=”POST”>
Instead of $_SERVER[“PHP_SELF”] you can also write like this:
<form name=”frm1″ action=”PHP_HTML_SINGLE_PAGE.php” method=”POST”>
<!-- Example : File name is : PHP_HTML_SINGLE_PAGE.php --> <html> <head> <title> HTML & PHP code On a Single Page</title> </head> <body> <form name="frm1" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST"> <p><strong> NAME : </strong> <input type="text" name="txtUser"> </p> <p><strong> Select Products : </strong> </p> <select name="products[]" multiple="multiple"> <option value="GoKey"> GoKey </option> <option value="Tripad"> Tripad </option> <option value="GoTenna"> GoTenna </option> <option value="SONY SMARTWATCH"> SONY SMARTWATCH </option> <option value="APPLE iPHONE"> APPLE iPHONE </option> </select> <p><input type="submit" value="Submit" /> </p> </form> </body> </html> <?php if(isset($_POST["txtUser"]) && !empty($_POST["txtUser"])) { echo "<p>Welcome <b>" . $_POST["txtUser"] . "</b>!</p>"; echo "<p>Your Product List are :</br>"; if(!empty($_POST["products"])) { echo "<ul>"; foreach ($_POST["products"] as $value) { echo "<li> $value </li>"; } echo "</ul>"; } } ?>