Advertisement
Tech

Using HTML forms and PHP to Set and Process Variables in Linux

It is time to use the 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!

By Berry van der Linden
Desk Tech
Reading time 3 min read
Word count 639
Linux Computing Linux introduction
Using HTML forms and PHP to Set and Process Variables in Linux
Advertisement
Quick Take

It is time to use the 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!

On this page

Creating the HTML form

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.

We’ll create a web page that takes a message from the user and shows it on a different page.

Advertisement

Lets begin with the HTML page that’ll contain the form. Call it “message_form.html” - if you want to call it something else, you may! For easy reading see this paste bin .

Advertisement

Get Input

Advertisement

Advertisement

What is your name?:

Advertisement


What is your message?

Advertisement



Advertisement

Advertisement

Explained…

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

Advertisement

The HTML

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?

Advertisement

creates another text field that creates a variable called “Message” that will store the users message.

creates a submit button, that the user will click.

Advertisement

ends the form.

So now we have a way of collecting the user’s input, we need to do something useful with it! You can use the echo or print command to output the variables contents on another page. In some cases it might be preferable to do all of this in one PHP script.

Advertisement

When the user clicks the submit button “message_tutorial.php” is loaded. Here is “message_tutorial.php” or see this code in this paste bin :

Advertisement

Here is your message

<?php

// Get the values from the form, “FirstName” and “Message”

$FirstName = $_REQUEST[‘FirstName’] ;

$Message = $_REQUEST[‘Message’] ;

?>

Hey,

Your message is

Do I use GET or POST?

You might have noticed that we used the POST method to pass data from the form to the PHP script

. However, we could have used GET instead.

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:

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

Notice the variable names and values being passed!

Conclusion

We now have a working form that passes variables to our PHP script. In the next part of this article series I will teach you about PHP arrays, this will help you to pass the form values to other scripts or to easily post the form values to a database.

This post is part of the series: A Guide to Linux Programming with PHP

In this series of articles I’ll be teaching you the basics of using PHP, and showing you why I think it’s the best language for web development. There are no pre-requisites for your level of knowledge, other than that you can understand basic HTML and are willing to learn.

  1. A Guide to Linux Programming with PHP
  2. A Guide to Linux Programming with PHP - Commenting and Executing Code
  3. A Guide to Linux Programming with PHP - Variables & Data Types
  4. A Guide to Linux Programming with PHP - HTML Forms and PHP
  5. A Guide to Linux Programming with PHP - Arrays
  6. A Guide to Linux Programming with PHP - Associative and Multidimensional Arrays
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux introduction
Advertisement