PHP: User Input
Handling HTML forms with PHP
Learn how to handle form input with PHP. Topics include HTML forms, GET/POST variables and output. If you made it through "PHP: Getting Started," then this tutorial is right for you!
INTRODUCTION
There are two basic concepts that drive every program ever created; input and output. Without these two essential components, programs would accomplish next to nothing. In "PHP: Getting Started", we covered basic output with the traditional "hello world" script. Truth be told, there would be no reason to create a PHP script to simply output static text -- that's HTML's job.
GOAL
In this script, we are going to output text based on a user's input, something that a strict HTML page can't do. So open Notepad++ and we'll get started.
WRITING THE SCRIPT
As always, we will need to begin with a <?php tag so that our script is processed as PHP code. Additionally, we are going to check for user input with a basic if statement.
On line two, you'll see that I use the PHP function strlen(). This function computes a given string's length, otherwise known as the number of characters present. I am essentially checking the variable $_GET['input'] (which will be the variable that contain's our user input) for the presence of text.
$_GET['{varname}'] is one of two ways to retrieve user input. The other method is $_POST['{varname}']. The two are very similar with one difference; get variables are appended to the end of a URL. If you ever see a URL with something like ?var=0&var2=1, then you've submitted a form using get variables. Post variables, on the other hand, are not present in the URL and are, for all intents and purposes, not visible to the user.
So, how do we set up the HTML form to send a get variable? Well, it's a lot easier than you may think.
On line one, we specify that the method is get rather than post. If all works as planned, everything that the user types should appear in the url following a ?input={user input}.
PUTTING IT ALL TOGETHER
So, combine the two and you should have something that looks like this:
PHP will pick up on the fact that there is data present after the user submits the form in the get variable "input" and will execute our IF block, thus returning You said: +the user's input.
And there you have it, your first dynamic PHP script! If you have any questions regarding the information in this tutorial, leave a comment!

Comments (4)
Ryan
exercise bike
Auth
greatest string quartets
post reminds me of my previous room mate! He always kept talking about this.
I will forward this post to him. Fairly certain he will have a good read.
Thank you for sharing!