How to fetch data from HTML to PHP using Single file


In my last article I will explained how we can fetch data from html to php in separate files. In this article I am going to explain how we can fetch data from html in the same file.

Let's take the same example where we have to take two numbers from the users. Create php file and add html code to create user interface.

Now I want when I click on submit button then my php code will start execution. So we have to use isset function to check whether form has been submitted or not.


We will add code in the following manner:-



echo "The sum of $num1 and $num2 is $sum";




<form action="sum.php" method="get">
Enter first number:-<input type="text" name="t1"/>
Enter Second number:-<input type="text" name="t2"/>

<input type="submit" value="Sum" name="b1"/>




Now execute this code and you will get following output:-

fetch data from html to php using single file

		Figure 1

HTML form to php output

		Figure 2

In this way we can fetch data from html controls in php.