ISSET Function in PHP

How to use PHP ISSET Function?


by re4nightwing

First of all if you haven't checked out the first part of php tutorial please go and check it out here it'll give you a good understanding about PHP basics.

Isset function is used to execute a block of  command when a specified button is clicked.
Today I'm going to show you how to use a HTML code and a PHP code inside one PHP file.

Step 1:

Make a PHP file (example.php) inside your localhost directory.

step 2:

<html>
<head></head>
<body>
<?php
if (isset($_POST['sub'])){
$name = $_POST['name'];
$age = $_POST['age'];
echo "your name is $name.<br>";
echo "and You're $age years old.";
echo"<form action='code.php' method='POST'>
<h1>Form</h1>
enter Your name : <input type ='text' name='name'><br>
enter your age : <input type ='text' name= 'age'><br>
<input type='submit' name='sub'>
</form>";
}
else{
echo"<form action='code.php' method='POST'>
<h1>Form</h1>
enter Your name : <input type ='text' name='name'><br>
enter your age : <input type ='text' name= 'age'><br>
<input type='submit' name='sub'>
</form>";}
?>
</body>
</html>
 In here I have used a if condition and as the condition I have used the isset($_POST('sub')). This means If the user has clicked on the 'sub' named button the commands under if are executed or else the commands under else are executed.

Under the else condition I have created a form using echo command. PHP engine can't process direct HTML code so we have to use echo or print commands to take the html output.

As you can see there is the same form code in the if condition too that means if you press the submit button then you get both of the result and the form again to insert a different Input.

If you run this for the first time You'll get get only the form because you have not clicked on the submit button so the else condition is activated.


First run:

first run result

 Give a input and click on the submit button:



submit button clicked

** you need a webserver software to run php.

     1.Xampp server - XAMPP Download

     2.Wampserver  - Wamp download

Post a Comment

0 Comments