Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Form Handling - GET vs POST.docx59.1 KB Form Handling - GET vs POST - Solution.docx
59.5 KB
Quick reference
Form Handling - GET vs POST
Forms are used on a website to allow site visitors to interact with you in some way.
When to use
Whenever you want to add a form to a webpage.
Instructions
Here's the basic HTML template I used in the video:
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Online Form</h1>
<form action="form.php" method="post">
Name: <input type="text" name="name"><br/><br/>
<input type="submit">
</form>
</body>
</html>
Hints & tips
- Forms send data from web pages
- Forms use POST and GET as methods
- POST variables look like $_POST["field_name"];
- GET variables look like $_GET["field_name"];
- 00:04 Okay, in this video I want to talk about online forms.
- 00:07 And just about every website has a form for some reason or other.
- 00:10 It could be a contact form,
- 00:12 it could be an e-mail newsletter sign-up form, all kids of different forms online.
- 00:16 So up until now, we've been talking about just doing php on a page.
- 00:21 But for the most part, whenever you're online,
- 00:23 you're gonna want to interact with the people that are on your website.
- 00:27 Or you're gonna want them to interact with you in some way or
- 00:30 with your website in some way.
- 00:31 And generally, online we do that with forums.
- 00:33 So we're gonna talk really quickly about setting up a quick php form for a website.
- 00:37 And I've taken our index page, and I've just created some very simple html.
- 00:42 If we come back here this is all that does.
- 00:44 It just put's this on the page.
- 00:45 And you don't need to know what any of this stuff is.
- 00:47 I'll put this in the resource file so you can copy and paste this if you want.
- 00:50 But now I'm just gonna paste in a quick form, and let's take a look at this and
- 00:55 see exactly what we have here.
- 00:57 So all forms start with this form tag, and they close with a form tag.
- 01:01 In this action this is the php file we're going to create, and
- 01:05 this forum will point to that file, right?
- 01:08 And then here we have method post, there's two methods when it comes to forms,
- 01:11 post and get.
- 01:12 And we'll talk about both of them very briefly here in just a minute.
- 01:15 So now we have this name and an input field.
- 01:17 We've called the input field name, and then we have a submit button.
- 01:20 So if we save this, come back here, and
- 01:22 hit Reload, you can see it's just a very basic form.
- 01:25 And if we type something we hit Submit,
- 01:27 nothing happens 'cause we haven't created this other form file yet.
- 01:30 So let's come up here and do that real quickly.
- 01:33 I'm just gonna New File, call it form.php, okay?
- 01:37 And now I'm just gonna copy all of this stuff, form.php, I'm gonna copy
- 01:42 all of this stuff and just paste it in here except I'm gonna remove the form.
- 01:47 And I'm just going to put a two on here, so we know this is the second form.
- 01:51 So now if we come back here and click submit, our second page comes up,
- 01:55 our form.php page, but nothing has happened.
- 01:58 Let's look at our index page.
- 01:59 In order to pass stuff from one file to another,
- 02:02 we have to know the name of this input field and we just named it name, right?
- 02:06 This is a post form.
- 02:08 So in order to access this information on the next page, we need to do this.
- 02:12 So, let's create a php tag like always, and let's just echo out,
- 02:19 You Entered and then this.
- 02:24 They kind of looks like a variable because it has a dollar sign,
- 02:28 but it with starts out with _POST, and then these brackets and then name.
- 02:32 And that name comes from this name.
- 02:35 If we would have named this name four and
- 02:37 over here we would put name four, but you get the idea.
- 02:41 So now if we save this and save this, come back to our website here and hit Reload.
- 02:47 You can type in John Elder, click Submit.
- 02:50 You Entered: John Elder.
- 02:52 So that's really cool.
- 02:53 Now one thing, notice up here in the address bar we have this form.php,
- 02:58 and that's all.
- 02:59 That's interesting, so now that brings us to the next part, get.
- 03:04 And we just change that from post to get and save it, and we come back here and
- 03:07 we change this to _GET, save that, come back here, hit reload.
- 03:15 I'm just going to type in John, click Submit.
- 03:19 You entered John, but now look up here.
- 03:21 It says name equals John, the parameters get passed right into the actual URL.
- 03:27 And there's lots of different reasons why you might want that,
- 03:29 there's a lot of different reasons why you might not want that.
- 03:32 If, for instance, this was an order form, right, and somebody was entering their
- 03:36 credit card information, you would not wanna use the get method.
- 03:39 Because their credit card is gonna appear up in this URL, and you don't want that.
- 03:43 So you would use post in that circumstance, and post hides all of these
- 03:47 different variables, all of these different data points that you send.
- 03:50 So that's a very, very quick intro to forms.
- 03:53 We're going to use forms a little bit later on, so
- 03:54 you get a little bit of practice with them.
- 03:56 But passing information around between pages is very important, and
- 03:59 this is a great, and easy way to do it.
- 04:01 Also, it's all websites have Order forms, newsletters, signup forms,
- 04:06 contact forms, and this is basically how you do that with php.
- 04:10 So in the next video, we're gonna talk about super globals.
Lesson notes are only available for subscribers.