Form and URL Variables

Okay using what we know from lesson 1 here is how to process a form…

Form Page:
<form action="processform.php" method="post">
Box 1: <input type="text" name="box1" /><br />
Box 2: <input type="text" name="box2" /><br />
Box 3: <input type="text" name="box3" /><br />
<input type="submit" name="submit" value="Submit" />

So we have three text boxes called box1, box2 and box3. After filling out the boxes and submitting the page it goes to a page called processform.php. The information stored in each box is stored under it’s variable name as specfied in the name attribute. As we have used the ‘post’ method we have to use the $_POST php superglobal to retrieve the value of each variable.

print "The Info in Box 1 is: ". $_POST['box1'] . "<br />";
print "The Info in Box 2 is: ". $_POST['box2'] . "<br />";
print "The Info in Box 3 is: ". $_POST['box3'] . "<br />";

This page is now purely dynamic. It relies on the information given in the text boxes to create it’s page.

If the method is ‘get’ or if the information is stored in the URL eg: then we use the $_GET php method to retrieve the variable information.

print "My name is ".$_GET['name'];

If we want to combine more than one variable in the URL we use the ampersand sign to do this eg. index.php?name=sarah&nat=English
However it is not wise to use sensitive date this way as it can easily be edited by someone. Simple rule of thumb, only use the URL line for basic simple variables that only do something such as determine which page to display or which results to display. Never use it to pass along a customer id or value etc.

You may also like...

1 Response

  1. abhi says:

    This Code really help me very much. so Very very thanks to you.