How to Use Post Variable in PHP


How to use Post variable in PHP? Mostly newbies trying to find the answer of this question. Lot of writers and bloggers have already written about it. Today I am trying to write some words about it too. May be somethings you haven’t learnt. How to use Post variable in PHP. It’s very simple and easy, you can do yourself too. just read this post till to end.

Use Post Variable in PHP :

Post variable is a most used variable in php, you can create forms to submit data in php with it.. It’s very easy to use, just you need to create two php files just like request and response file. Whatever names could be of request and response files such as bio-form.php and bio-form-res.php as I have created.

Post Variable in PHP
Submit data with Post Method

bio-form.php:

Just create a form with attribute value Post of the method.

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

Just create some input fields to enter data for submit. I have created four input fields such as first name, last name, age and date of birth.

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>

Now close form tag of html.

</form>

Friends you can see these is no PHP code in bio-form.php. You can have the name of the file bio-form.html too. Actually I don’t do this, usually I create both request and response files in PHP format. But there is no issue whether you have created it in html, both will work same.

In bio-form.php, I have just create a form with four input elements, and haven’t defined default value of input tags. Then these is a submit button to submit the information provided with this form.

bio-form-res.php:

First start PHP script with PHP opening tags like this.

<?php

Now print input fields with pre defined variable $_POST. I have separated each field with line break whether you want to separate or not it’s depend upon you.

echo $_POST["first-name"];
echo "<br />";

echo $_POST["last-name"];
echo "<br />";

echo $_POST["age"];
echo "<br />";

echo $_POST["date-of-birth"];

Now close PHP script with closing tag of PHP.

?>

Now response page bio-form-res.php is done. If you want to use html anchor tag to go back to the request page like bio-form.php for submit another data, you can use.

<a class="nav-link" href="http://www.howtodecode.com/test/bio-form.php">Go Back</a>

Dear friends in above short coding you can see, there are only seven lines, written in php. Such as opening PHP tag, print input fields with pre defined variable Post and closing PHP tag.

Note:

Don’t forget the main point, in this guide, just you have learnt to submit a form from html and get values of this form in PHP. It doesn’t mean that your data will be saved in mysql, mysqli, oracle or any other database. At this stage you are just retrieving the data from HTML with form.

Dear friends if you don’t want to type coding, and want to test this example, you can check it in my website www.howtodecode.com too. Just click on the link below.

http://www.howtodecode.com/test/bio-form.php

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

Dear friends if your website is on WordPress, don’t forget to secure your site, It’s very important for you. If you don’t know how to secure it, I had read an article about it before some days, today I am share it with you, I hope it will be helpful for you too. just read this post Two Ways to Secure wp-config.php File.

Dear friends if don’t understand anything, feel free to contact with me, I am here to assist you at any time.


Leave a Reply

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