A Guide to Linux Programming with PHP - HTML Forms and PHP

Article by Josef Nankivell (4,324 pts ) , published Aug 23, 2009

It is time to use your awesome skills you have obtained over the last few articles. In this article we'll use PHP and an HTML form to collect data from the user, store it in variables and then process it using PHP. As this is your first go we will use a simple example and move on to greater things!

Need Input!

In previous articles, we talked about PHP and its syntax, created a working script, executed it and looked at variables. Now lets put some of this to use and create an HTML form that we will use to collect input from the user. We'll then use variables to store the input from the user and then perform awesome tricks with it using PHP.

That's enough talking! We'll now start to make a web page that takes a message from the user and shows it on a different page.

We'll begin with the HTML page that'll contain the form. Let's call it "message_form.html" - if you really want to call it something else, you may!

<html>

<head>

<title>Get Input</title>

</head>

<body>

<form action="message_tutorial.php" method=post>

What is your name?:

<br> <input type="text" name="FirstName">

What is your message?

<br /><input type="text" name="Message">

<input type="submit" name="submit" value="Submit">

</form>

</body>

</html>

Explained...

Above is just a plain old static HTML form, below points out the juicy bits:

The HTML <form action="message_tutorial.php" method=post> dictates which PHP script will process the input from the from, in other words, it's what makes the form work or do anything at all! Post is the method in which the data is sent from the form to the PHP script; we shall look at this later.

Input type="text" creates a text box element on the form (where the user inputs their data). Name="FirstName" - The data that the user inputs here will be stored in a variable called "YourName." Can you start to see the importance of variables in forms now?

<input type="text" name="Message"> creates another text field that creates a variable called "Message" that will store the users message.

<input type="submit" name="submit" value="Submit"> creates a submit button, that the user will click.

</form> ends the form.

So now we have a way of collecting the user's input, we need to do something useful with it! We'll start with something simple and use the echo command to output the variables contents on another page.

When the user clicks the submit button "message_tutorial.php" is loaded. Here is "message_tutorial.php" :

<html>

<head>

<title>Here is your message</title>

</head>

<?php

// Get the values from the form, "FirstName" and "Message"

$FirstName = $_REQUEST['FirstName'] ;

$Message = $_REQUEST['Message'] ;

?>

<p>

Hey, <?php print $FirstName; ?>

<p>

Your message is <b> <?php echo $Message; ?></b>

<p>

</body>

</html>

Do I use GET or POST?

If you have been paying attention, you should have noticed that we used the POST method to pass data from the form to the PHP script <form action="message_tutorial.php" method=post>. However, we could have used GET instead.

Woah! What is the difference between POST and GET? Well, POST is sent as plain text in the body of the request and is not visible to the user, whereas, GET will send the information as plain text in part of the URL, for example:

http://localhost/message_tutorial.php?FirstName=Josef&Message=Hello%21&submit=Submit%21

Notice the variable names and values being passed!

</form>

Congratulations on passing variables from a form to a PHP script! We are now starting to get up to speed with PHP, next time we shall look at some PHP arrays. Don't worry, I shall hold your hand and walk you through!

 
Subscribe to Linux
RSS
Get free weekly updates, directly to your inbox.
Browse Linux Platform