What Will I Learn?
- You will learn how to receive data from a form using php via both GET and POST method
Requirements
- LAMP/WAMP/MAMP/XAMP or any other mysql/php installation stack
Difficulty
- Intermediate
Tutorial Contents
Step1 : Create an HTML Page to contain the form
<form action="/action_page.php" method="post">
Name:<br>
<input type="text" name="firstname"><br>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
<input type="submit" name="go" value="Submit">
</form>
action
refers to the page that will be processing the form, in a situation where you want the form to be processed on the same page you can simply change that first line to this<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
.<?=$_SERVER['PHP_SELF']?>
is just a way of showing the web url of that page.method="post"
refers to the way in which the form will be processed. There are two ways of processing forms in html which would also be reflective in the PHP processing in two ways :method="post"
ormethod="get"
<input type="text" name="firstname">
is just a normal html input , the most important part of this html tag is the name element which isfirstname
, that is what is needed in the php processing file.
I will assume that you have an intermediate understanding of how to use html. By that, you should know that all input tags have a name element attached to it.
Since all input tags are virtually treated the same way, let's jump to the input['type=submit'] tag.
<input type="submit" value="Submit">
: The most important of this is the value tag which is very important in the processing of the form. keep that in mind, more so, take cognizance of the case of thevalue
which is a major cause of problems in form processing.
Step2 : Receive the data using POST method
Assuming we are using method="post"
:
When the user clicks the submit button, the form carries all the data submitted and takes it to the action page which can either be the same form page ,as I stated earlier on or another page depending on what stated on the action page. So everything stated here will be on the action page. let's assume the action page is "action.html".
Take note : every data submitted in the html form will be reflected here as an element in the $_POST array. So, this input element <input type="text" name="firstname">
will be reflected as $_POST['firstname'] including the submit input tag too.
action.html
if (!empty($_POST['go'])){ var_dump($_POST)}else {echo 'error';}
!empty($_POST['go'])
is to ensure that a value was sent from the submit button before the php codes in the curly bracket was executed.
Receive Data using the Get method
Using the get method is not as secure as using the POST as all the form data will be attached to the url while posting. For instance, if we are to submit the form stated above , the url will look like on this while loading to the action page example.com/form.php?firstname=&gender=male&go=Submit
, that is assuming the form page is form.php
and the website is example.com
below is how the action page will receive it
action.html
if (!empty($_GET{
var_dump($_GET)}
Comparing it to the POST method, the way to make the code secure against unnecessary intrusion is by checking if the $_GET array is not empty, meaning someone can just edit the url and then intrude.
Curriculum
This tutorial is the first of a series , the next tutorial will be how to format form data
which will teach how to format form data to avoid problems when sending to database
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
However, please include more content into tutorials next time.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @shreyasgune, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks. I definitely will
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Really nice to see a lot of php contributions here on utopian, thanks to people like you. Nice one really!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Yeah. Seems like there isn't a lot of
php people
around here. ThanksDownvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @akintunde I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit