How to Use Get Variable in PHP to Submit Data

How to use Get variable in PHP to submit data? That’s the question, mostly newbies want to know the answer. Because mostly newbies use Post method to submit data in PHP. The key function of Get and Post is same, but they work slightly in a different way. If you can understand the difference between Post and Get. I think you can use suitable method according to your demand. If you don’t know how to use Get variable in PHP, don’t worry. I am here to give you enough information about it. Let’s move to learn it now.

Get Variable in PHP
Submit form with Get Method

Use Get Variable in PHP:

As you know PHP works on request and response method. Whatever we have a task to accomplish, we need to create two files request file and response file. In this example my request file is bio-form-get.php and response file is bio-form-get-res.php. I am going to show you structure of both files.

bio-form-get.php:

Step 1:

Start a form tag of html with Get method, as I have created, it means we use Get as value of method attribute.

<form action = "bio-form-get-res.php" method = "get" >

You can see I have pointed file bio-form-get-res.php , it means when a user submit this form, the data of the form will go to the file bio-form-get-res.php, you can have what ever file you want. It will be placed in action attribute as value, as I have.

Step 2:

Now create some fields, as I have created four input fields like first name, last name, age and date of birth. I have separated them with <hr> tag of html, it will separate each input field with a line in resulting page.

Enter First Name <input type="text" name="first-name"/>
<hr>
Enter Last Name <input type="text" name="last-name"/>
<hr>
Enter Age <input type="text" name="age"/>
<hr>
Enter Date of Birth <input type="text" name="date-of-birth"/>
<hr>

Step 2:

Now create a submit button to submit data of the form to process for further use, as I have created and close form tag of html.

<input type="submit" name="submit01">
</form>

Friends you can see first I have start form tag, then create four input fields to allow user to data for submitting and lastly close form with submit button. You can see all of the coding above at one place as I have done here for you.

<form action = "bio-form-get-res.php" method = "get" >
Enter First Name <input type="text" name="first-name"/>
<hr>
Enter Last Name <input type="text" name="last-name"/>
<hr>
Enter Age <input type="text" name="age"/>
<hr>
Enter Date of Birth <input type="text" name="date-of-birth"/>
<hr>

<input type="submit" name="submit01">
</form>

bio-form-get-res.php:

Step 1:

Start PHP script with php opening tags, as I have started here.

<?php

Step 2:

Now just print pre defined variables with echo statement as I have done for you, you can see I have separated each value with break line. And at the last line there is an html anchor tag to go back after submitting data to the bio-form-get.php page t submit another data.

echo $_GET["first-name"];
echo "<br />";
echo $_GET["last-name"];
echo "<br />";
echo $_GET["age"];
echo "<br />";
echo $_GET["date-of-birth"];

Step 3:

Now close PHP script with closing tag of PHP, as I have done.

?>

In above code snippets I I have created some PHP coding to get data from form i. e. request page bio-form-get.php,you can see complete coding once again to understand.

<?php
echo $_GET["first-name"];
echo "<br />";
echo $_GET["last-name"];
echo "<br />";
echo $_GET["age"];
echo "<br />";
echo $_GET["date-of-birth"];
?>

If you don’t want to type coding of this example, you can check results of these files from my server too. Just you can visit the following page, type values in fields and submit the form. You can see the results in response page i.e. bio-form-get-res.php.

https://howtodecode.com/test/bio-form-get.php

Note:

You can notice that when you are submitting data from the page bio-form-get.php. It will go to the response page like bio-form-get-res.php and URL of the page look like this

https://howtodecode.com/test/bio-form-get-res.php?first-name=kamran&last-name=Khan&age=44&date-of-birth=4-01-1920&submit01=Submit

You can see all of the data you filled in form, is shown in the URL. If you don’t want to show filled data in URL. You need to use Post method of PHP. If you don’t know how to use Post method in PHP, just read this post. I have already written this post for you.

Dear friends if you don’t know how to declare variables in PHP, don’t worry, I have already written a post on this topic too, just you can visit this link How to Declare Variable in PHP

If you want to start a video sharing website in php, I recommend you these scripts to you, I hope you will enjoy. Best 10 PHP Scripts to Start Video Sharing Site

Leave a Reply

Your email address will not be published. Required fields are marked *