Form processing using PHP

Create a simple form in the index.php page we earlier, below is the source.

index.php

<HTML>
<HEAD>
<TITLE> Testing PHP </TITLE>
</HEAD>
<BODY>
<form method="post" action="process.php">
  <input type="text" name="country" >
  <input type="submit" name="button" value="Submit">
</form>
<p>
<?php
print "Today's date is: ". date('d-m-y');
?>
</p>
</BODY>
</HTML>


I bolded three of the most important attributes of the form:

first is the method=”post”, this indicates the method we want the form to use in submitting its data, therefore we have to use the PHP $_POST special variable to access its data likewise.

Secondly the action="process.php" which tells the form where to post the data to, in this case a page name process.php, which we will create next and will contain some PHP code to handle and respond to the form.

Then thirdly, name="country" this is actually the name of the textbox or text field, to collect or respond to this textbox, that is process or display it value, we will have to reference this name using the form post method as we have here $_POST[“country”].

Now let’s create the process.php page in the same “test” website where we have the index.php, to make this process easy, I decided to define the website using Dreamweaver, below is the screenshots. See Dreamweaver chapter, to see the steps.

test web

After defining the website, we now have the screenshot below, after I’ve open the index.php page and switch to code view. Please note, that you can ignore this, if you’re not using Dreamweaver and just create the process.php just as you did with the index.php

web open

Now create the process.php page and let it contains the code below.

Note: If you are using Dreamweaver, beware that Dreamweaver will have some other tags, but your concern should be only to add the PHP code.

process.php
<HTML>
<HEAD>
<TITLE> Testing PHP </TITLE>
</HEAD>
<BODY>
<?php
//assign the value of the textfield to a variable
$val = $_POST["country"];

//display the some msg and the value of the variable
print "Your from ". $val;
?>
</BODY>
</HTML>


The PHP started with a comment line; recall that JavaScript that comment is a non-executable line that helps the programmer to describe what a program line of section is doing, for easy maintenance. As you can see in the program above that the first comment tell you what the line below is doing, which is to collect value from the “country” text field submitted with POST method and assign or store this value to the $val variable.

And the next line also have a comment, describing what the line below it is doing, I thinks this comment describe this line better so I don’t have to tell you what it does again. Now let’s test this.

Open the index.php in a browser as we did earlier, and enter “Nigeria” into the text field and submit, this will post that data to the process.php page, which will display a message with the value you enter. See the screenshots below.

both

As you can see, it is that easy, now recall that we said PHP is loosely typed language, that has come to play a role here, because we entered a text the $val variable stored the value as String, similarly if we had enter a number let’s say 5 this same variable will behave like an Integer or we entered 10.2 it would handle it as a Double or Float, as I said this is what you enjoy only in a language like PHP but not in Java or C# because they are tightly typed language, I need not go further if you are interested in this difference or feature you can go to the internet and search.
  
Lets modify process.php, to multiply the value of whatever we have in the $val variable by itself. 

process.php
<HTML>
<HEAD>
<TITLE> Testing PHP </TITLE>
</HEAD>
<BODY>

<?php
//assign the value of the textfield to a variable
$val = $_POST["country"];

//multiply the value by itself 
$val_mult = $val * $val;

//display the some msg and the value of the variable
print $val . " multiply by $val is: ". $val_mult;
?>
</BODY>
</HTML>


The first bold line uses a multiplication (arithmetic) operator to multiply the variable $val by itself  $val * $val and store the result it a new variable $val_mult, while the next or last bolded line does the display or output. Note however that the show the print or output line concatenate or joint some variable to the string message using a period or dot (.)

$val . "multiply…". $val_mult which is the normal and expected in other “strongly typed” programming languages, but you could also notice a strange variable right inside the string not concatenated " multiply by $val is: "  yet PHP still considered this a no problem, this is another feature of a loosely typed language like PHP, no wonder another popular PHP book called the language a forgiving language.

To test this, enter a number into the text field in the form and submit, for example I enter 50 and submit see the screenshot below.

process 

As I said earlier, that this chapter of the book won’t go into detail explaining Operators, Decision and Looping in PHP because It just the same with that of JavaScript and since we’ve treated all this in JavaScript I’m expecting you to must have gone through that.

This tutorial is more about showing you some useful and simple task you may likely be handling with PHP, such as a program to send emails, connecting to a MySQL database, querying the database and a simple user authentication program, which could give your PHP career a boost.

 

Next »


Introduction to PHP and MySQL »

Setup PHP and MySQL »

Starting WAMP Server »

Testing the WAMPServer (First PHP Program) »

PHP Code block and Variables »

Form processing using PHP »

Sending e-mail with PHP »

PHP Interaction with a Database »

SAMPLE1: a Feedback system »

SAMPLE2: Authentication system »

More PHP sample code »