Locked lesson.
About this lesson
Let's build out the form to make it work.
Exercise files
Download this lesson’s related exercise files.
Making The Form Work.docx59.2 KB Making The Form Work - Solution.docx
60.2 KB
Quick reference
Making the Form Work
Let's make our form actually post data.
When to use
Anytime you have a form, it should do something!
Instructions
To create hidden form fields:
<input name="num1" type="hidden" value="<?php echo $num1; ?>">
<input name="num2" type="hidden" value="<?php echo $num2; ?>">
To test whether or not the form has been submitted:
if (!$_POST["num1"]) {} else {
echo $_POST["num1"] . " + " . $_POST["num2"] . " = " . $_POST["answer"];
}
Hints & tips
- $_POST["answer"];
- <input name="num1" type="hidden" value="<?php echo $num1; ?>">
- 00:04 We've got a form but it doesn't do anything yet.
- 00:07 So in this video, let's work on that functionality.
- 00:09 So when we click Submit, we know from back when we learned about forms,
- 00:13 we can pass whatever's in this field here with the, we can use this
- 00:18 super global of post with answer because that's what we named this thing.
- 00:23 So let's come up here to, let's see, here is where our numbers are output.
- 00:30 Let's just create a PHP tag and let's just echo out that thing.
- 00:36 So if we save this, come back here and hit reload, you see there's nothing there.
- 00:40 If we submit, say 6, boom, it hits six.
- 00:44 That seems to work.
- 00:46 But whenever we do that, these numbers change, and we can't really have that.
- 00:51 So I'm gonna show you a neat trick that we can use to pass
- 00:54 variables between pages with forms.
- 00:58 What we're gonna do is create a couple of input fields.
- 01:02 And let's name the first one num1, and give it a type="hidden".
- 01:10 Now, if we copy this, well actually, and let's give it a value equal to.
- 01:15 And let's create a PHP tag right here in the form, we can do that.
- 01:20 And let's echo out our num1 variable from up here.
- 01:25 So if we save this, or actually first before we do that, let's copy and
- 01:29 paste another one, and let's do num2, and we'll do the same thing here.
- 01:34 And actually let's give a space here, okay.
- 01:36 Now if we save this, come up here and hit reload, 2 and 8 are our numbers.
- 01:40 If we view the page source, we can see this hidden fields have two and
- 01:45 eight as the values but they're hidden so we can't actually see them on the screen.
- 01:50 But, that allows us to now pass these values to a new super global
- 01:54 just like we did with our answer.
- 01:56 So, let's copy this and let's paste out those two super globals.
- 02:01 So, the first one is num1 and then let's concatenate.
- 02:07 And put + and then concatenate again and put our second number,
- 02:13 num2 and another concatenation = and then our final concatenation.
- 02:19 Now let's save this, come back here and hit reload.
- 02:23 So, let's start from the beginning here.
- 02:25 So three plus four equals seven.
- 02:27 We submit, three plus four equals seven.
- 02:30 But there's something weird going on here.
- 02:32 When we come here the first time, this stuff is output.
- 02:35 Well that's because the first time we come here we haven't submitted these things,
- 02:40 so there's nothing to post.
- 02:42 So we can change that with a simple if statement.
- 02:44 Let's go if, and I'll just grab one of these.
- 02:51 If there is no num1 being posted,
- 02:55 then do nothing, else do all this stuff.
- 03:01 I'll save that, come back here and hit reload.
- 03:04 Nothing has been posted so nothing gets listed here.
- 03:07 But if we type something, 9 + 3 = 12.
- 03:11 Boom, it works.
- 03:12 But then again, up here, we're starting to get two different numbers but that's fine.
- 03:17 So we now have our form that's functional.
- 03:20 In the next video, we're gonna look at creating some logic to determine whether
- 03:25 this is a correct answer or not, and
- 03:27 then outputting a different message based on whether it's correct or incorrect.
- 03:31 So that's all for this video.
Lesson notes are only available for subscribers.