PHP Forms Tutorial

Forms are the heart of web programming. They provide a way of accepting input from a user and sending it to a server. There are two methods that you can use to send data to a server, the get method and the post method.

The get method appends the data you are sending to the URL you are sending it to, while the post method sends it out of view in the HTTP request.

So for example, if we were building a login form, we would send the username and password using the POST method as we would not want such sensitive information to be appended to the URL (and be in plain sight) once we sent it.

Processing Form Data with PHP

Sending data via get to a URL may look like:

/index.php?name=beginner&school=mannings

As you can see above, the data is directly added to the URL.

Let's dive into an example that uses the post method:

<html>
<body>
 
<form action="test.php" method="post">
Name: <input type="text" name="name" />
School: <input type="text" name="school" />
<input type="submit" />
</form>
 
</body>
</html>

If the user were to fill in the form and then press the submit button, the data will be passed using the post method (which we look at later) to a script called "test.php". Now, let's say "test.php" looks like this:

test.php:

<html>
<body>
 
Welcome <?php echo $_POST["name"]; ?>...<br />
You attend <?php echo $_POST["school"]; ?>.
 
</body>
</html> 

The output may look something like:

Welcome John...
You attend Mannings School.

That's all there is to passing data to a server and have the server manipulate it and generate some sort of output based on it.

Now, go on to our PHP $_GET Tutorial or PHP $_POST Tutorial to analyse the different methods of sending data to the server.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...