PHP Forms [Aug. 10, 2011, 3:43 a.m.]
Forms is where you start to actually have some fun! This is where you start your journey from static webpages, to dynamic webpages. PHP Forms is why we've had to go through all the previous classes and learn some, not-so-fun stuff.
To catch everybody up, so we're all on the same level right now, a form is what holds those little boxes on websites that you put your information on.
ie: registration form has your name, email and password.
Up until now, any static web site you've made most likely can't do anything with a form. For now, we're going to stick with the two basic types of forms: GET and POST.
Let's move in to our first section,
Post Method
Save the code below as info.html
<html>
<body><form method='post' action='do.php'>
What is your name? <input type='text' name='my_name' /><br />
What is your favorite color? <input type='text' name='fav_color' /><br />
<input type='submit' />
</form></body>
</html>
The red areas above are the parts that are probably new to you.
Method is the type of form we use, GET or POST.
Action is the file we want to execute when someone hit the submit button.
Name is what we will be naming the variable the PHP makes for us.
These 3 keys are extremely important!
Now copy and save the code below as do.php:
<html>
<body>Hello <?=$_POST['my_name']; // shorthand echo command ?> who likes the color <?=$_POST['fav_color']; // shorthand echo command ?>
</body>
</html>
Upload do.php and info.html into the same directory, and open http://yourwebsite.com/info.html
Type in your information and hit "submit".
After you hit the submit button, notice your URL changed to do.php, and it has your information you put inside the form!
But wait.. if you just went to http://yourwebsite.com/do.php, your page would display: Hello who likes the color. Why does it do this?
The $_POST superglobal variable is only created when you submit the form.
If you didn't notice, the name you gave your input field is what your $_POST variable was named.
Why should I use $_POST?
$_POST is more secure than $_GET, because $_GET (or method="get") urlencodes the inputs.
Get Method
Like I said, the main difference between $_POST and $_GET is that $_GET turns your inputs into a URL.
Open info.html again, and change the form method to get, and save it.
Open do.php again, and change $_POST['my_name'] to$_GET['my_name']
and change $_POST['fav_color'] to$_GET['fav_colour'].
Upload both files, and open info.html in your browser.
Input your information and hit submit..... Looks like there's no difference..
But if you look at your URL, it should say something like http://yourwebsite/do.php?my_name=dave&fav_color=red.
After do.php is a question mark "?", followed by your form's field name this is equal to your input. The AND "&" symbol in your URL means it's connected another $_GET variable to your page.
The "?" means that PHP will start looking for a variable in the URL.
ie: yourpage.php?variable=value&variable2=second_value
<form method='get' action='do.php'>The GET method
What is your name? <input type='text' name='my_name' /><br /> my_name is in the URL
What is your favorite color? <input type='text' name='fav_color' /><br /> fav_color is in the URL
<input type='submit' />
</form>
Try this: change the last part of your URL.
ie: change red to PHP+Forms (Read a little on urlencoding and urldecoding if you can't wait to figure out why the "+" symbol made a space)
Your page changes!! As easy as that!
Another Option:
Instead of using $_POST and $_GET, you can also use $_REQUEST. $_REQUEST can get information from get and post methods, and even cookies.
Your task:
Create a different html page that has a form with 3 or more input fields.
When you "submit" your form, the new page will echo the text you gave the form.
Do this using the get method, and the post method.
Try changing the URL