Passing Data to PHP

Workshop Requirements

Before beginning this workshop ensure that you have the facility to upload PHP files to a PHP enabled webserver.

You should also have completed the following Virtual Workshops:

You should also have some experience with HTML programming and using HTML Forms.

Passing Variables to a script

When we were learning about variables and arrays in the previous workshop we were assigning values values statically as part of the code. This is obviously not very helpful in any 'real world' application, as we need to be able to pass different values to a PHP script, which are then assigned to our variables. There are two ways we can pass these values; as part of the URL or from an HTML form.

Variables as Part of a URL

If we look at the normal URL of a file (for example this one) it will look something like this:

http://www.keithjbrown.co.uk/vworks/php/php_p2.php

This is asking the webserver if we can download and view a a file called php_p2.php . However more information can be passed to that file as part of the URL. This can quite often be seen as part of the URLs that search engines use when displaying results. For example searching for 'PHP' using Google produces this URL:

http://www.google.com/search?hl=en&q=PHP

What we can see is that a file called search is being asked for and then there are variables being passed as part of what is known as the QUERY_STRING which the script will use to search the database of stored websites for 'PHP'. The QUERY_STRING is the part of the URL after the '?' and contains a series of name/value pairs separated by an ampersand ('&'). Thus the Google query string is...

hl=en&q=PHP

...which contains the following variables and values:

Variable Name Value
hl en
q PHP

So the next stage for us it to create a PHP file that will recognise variables that are passed to it via the query string. Create a new file called variables.php and put the following code into the file:

<html>
<head>
<title>My Variables PHP Script</title>
</head>
<body>
<p>The query string is: <?php echo $_SERVER['QUERY_STRING']; ?></p>
</body>
</html>

As you can see we are trying to echo the $_SERVER array member QUERY_STRING, which is a predefined variable. Save and upload this file to your webserver and then view the file. You will see the text 'The query string is:' but nothing else as we haven't included anything in the query string. The QUERY_STRING is a blank value as there is nothing after the filename as part of the URL and thus doesn't display anything when we echo it into the HTML.

The QUERY_STRING

In order to create a QUERY_STRING enter the following after your file name when you view the file:

variables.php?firstname=keith&lastname=brown

As we have included a QUERY_STRING, the text that will be displayed is now 'The query string is: firstname=keith&lastname=brown'. Change the URL so that it reflects your name and watch the output change. This method of transferring data to a script is known as the 'GET' method, and one of several requests that a webserver understands.

Knowing the QUERY_STRING is, again, of little use in real world applications, however it is a starting point for most scripting languages which then split this query string into name/value variables. As PHP had been designed with the web in mind it automatically puts all the names (keys) and values into an associative array called $_GET from which we can retrieve our data. We do this the same as other associative array, by specifying a Key (in this case the name) to retrieve the value. To illustrate we can now change our variables.php script to echo these array members (firstname and lastname).

<html>
<head>
<title>My Variables PHP Script</title>
</head>
<body>
<p>The query string is: <?php echo $_SERVER['QUERY_STRING']; ?></p>
<p>Firstname: <? echo $_GET['firstname'?></p>
<p>Lastname: <? echo $_GET['lastname']; ?></p>
</body>
</html>

You can now see how this could begin to become useful. However typing in pairs of variables and values as part of the URL isn't very user-friendly and thus we will now look at using standard HTML form fields to collect values.

HTML Forms

To begin our look at forms we will create a file called myvars.htm which contains an HTML form that is set up to send the data it collects to our variables.php file.

<html>
<head>
<title>My Variables HTML Form</title>
</head>
<body>
<h4>My HTML Form</h4>
<form name="myform" method="get" action="variables.php">
Enter Firstname: <input type="text" name="firstname"><br>
Enter Lastname: <input type="text" name="lastname"><br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

The important parts of this are that the 'action' attribute is pointing towards the correct file (variables.php) and that the names given to each text input field are the same as the array elements in the script, otherwise nothing will happen. Upload this file, try typing different names, and look at the effect on the output. You will also notice that there is still a QUERY_STRING in the URL when we submit the form. This is because we have used the 'GET' method. There is another that we will look at and use, the POST method.

The 'POST' method includes its information in the 'body' of a http request rather than in the URL and thus cannot be seen. We can see this in action if we change the method in our myvars.htm file

<form name="myform" method="post" action="variables.php">

When the form is submitted to the PHP script, nothing happens. That is because our script is using the $_GET array which stores values passed via the GET method, rather the $_POST array (that stores the values passed by POST). Thus we need to change this in the variables.php script to accept POST values.

<html>
<head>
<title>My Variables PHP Script</title>
</head>
<body>
<p>The query string is: <?php echo $_SERVER['QUERY_STRING']; ?></p>
<p>Firstname: <? echo $_POST['firstname'?></p>
<p>Lastname: <? echo $_POST['lastname']; ?></p>
</body>
</html>

The script should now work again.

Using $_REQUEST

There is a third array that stores values. If we want the values to be available no matter which method was used to pass the variables then we could use the $_REQUEST array (which combines the members from both $_GET and $_POST).

<html>
<head>
<title>My Variables PHP Script</title>
</head>
<body>
<p>The query string is: <?php echo $_SERVER['QUERY_STRING']; ?></p>
<p>Firstname: <? echo $_REQUEST['firstname'?></p>
<p>Lastname: <? echo $_REQUEST['lastname']; ?></p>
</body>
</html>

Mini-Exercise

Add other HTML form elements that you may have used before to the myvars.htm file and then using the echo function, print them to the screen when posted to the variables.php file.

Further Reading

Lastest 10 Threads - view all

musica vespa libro tutto quel ispira

Posted By: btrdl at 09:16:06 on Tuesday the 9th of October 2007

normativa impatto acustico spettacolo musica [URL= http://www.megalab.it/calcetto/.Pic/normativa-impatto-acustico-spettacolo-musica.html ]normativa impatto acustico spettacolo musica[/URL] information

niccolai grandi magazzino della musica [URL= http://www.megalab.it/calcetto/.Pic/niccolai-grandi-magazzino-della-musica.html ]niccolai grandi magazzino della musica[/URL] about

about natale musica musica dei cielo [URL= http://www.megalab.it/calcetto/.Pic/natale-musica-musica-dei-cielo.html ]natale musica musica dei cielo[/URL]

about musical testo musica aggiungi posto tavola information [URL= http://www.megalab.it/calcetto/.Pic/musical-testo-musica-aggiungi-posto-tavola.html ]musical testo musica aggiungi posto tavola information[/URL]

musica windows live space [URL= http://www.megalab.it/calcetto/.Pic/musica-windows-live-space.html ]musica windows live space[/URL] about

about negozio musica emilia romagna [URL= http://www.megalab.it/calcetto/.Pic/negozio-musica-emilia-romagna.html ]negozio musica emilia romagna[/URL]

about musica video matrimoniale [URL= http://www.megalab.it/calcetto/.Pic/musica-video-matrimoniale.html ]musica video matrimoniale[/URL]

about negozio musica aosta information [URL= http://www.megalab.it/calcetto/.Pic/negozio-musica-aosta.html ]negozio musica aosta information[/URL]

for more info click to nansy ajram musica [URL= http://www.megalab.it/calcetto/.Pic/nansy-ajram-musica.html ]nansy ajram musica[/URL]

for more info click to napoli musica live [URL= http://www.megalab.it/calcetto/.Pic/napoli-musica-live.html ]napoli musica live[/URL]

about musica x blog information [URL= http://www.megalab.it/calcetto/.Pic/musica-x-blog.html ]musica x blog information[/URL]

noleggia film musica online [URL= http://www.megalab.it/calcetto/.Pic/noleggia-film-musica-online.html ]noleggia film musica online[/URL] about

musica windows media player 5 0 [URL= http://www.megalab.it/calcetto/.Pic/musica-windows-media-player-5-0.html ]musica windows media player 5 0[/URL] information

negozio musica padova [URL= http://www.megalab.it/calcetto/.Pic/negozio-musica-padova.html ]negozio musica padova[/URL] about

origine musica classica italia [URL= http://www.megalab.it/calcetto/.Pic/origine-musica-classica-italia.html ]origine musica classica italia[/URL] about

about origine musica jazz [URL= http://www.megalab.it/calcetto/.Pic/origine-musica-jazz.html ]origine musica jazz[/URL]

for more info click to ok musica com [URL= http://www.megalab.it/calcetto/.Pic/ok-musica-com.html ]ok musica com[/URL]

about novecento didattica musica information [URL= http://www.megalab.it/calcetto/.Pic/novecento-didattica-musica.html ]novecento didattica musica information[/URL]

where get origine musica house info [URL= http://www.megalab.it/calcetto/.Pic/origine-musica-house.html ]origine musica house info[/URL] ?

find more about negozio musica sardegna here [URL= http://www.megalab.it/calcetto/.Pic/negozio-musica-sardegna.html ]negozio musica sardegna here[/URL]

You have built a good website%

Reply to this comment

Agree with other posters

Posted By: Bob B at 22:04:01 on Thursday the 13th of September 2007

awesome page

Reply to this comment

Great

Posted By: Ermias at 08:25:47 on Monday the 10th of September 2007

Amazing site. Better by miles than all the books I bought on Php and Mysql. Keep up the good work.

Reply to this comment

Passing Variables With URL

Posted By: Steven Pearl at 16:48:43 on Wednesday the 5th of September 2007

In your examples you show how you can use an HTML form to pass variables FROM the form to a URL. How would I do the opposite where I want the URL to pass the variables (i.e firstname, lastname) TO a form that has those corresponding fields?

Thanks, Steve

Reply to this comment

Hard to find but valuable info

Posted By: Andrei at 03:34:30 on Thursday the 30th of August 2007

After an exhaustive Google search I finaly found what I was looking for. Magnificent site, helped me out a lot.

Wish this site was better positioned, but maybe I did not find it inmediatelly because I wasn't exactly shure what I was searching for.

Like someone above said: Better than the book I bought :(.

Reply to this comment

canada-online-pharmacy-viagra

Posted By: pomn at 02:27:32 on Wednesday the 15th of August 2007

Looking for information and found it at this great site…r

Reply to this comment

ya mum

Posted By: ya mum at 01:14:25 on Thursday the 9th of August 2007

ya mum is hot

Reply to this comment

zmaxhixedb

Posted By: zmaxhixedb at 02:51:44 on Wednesday the 1st of August 2007

Hello! Good Site! Thanks you! rspxuvvsbmlkdh

Reply to this comment

Nicely done

Posted By: Andrew at 16:23:20 on Friday the 6th of July 2007

Concise, precise, using a hands-on approach with valid code. Better than the book I bought :(. Wish I'd come here first.

Reply to this comment


Post a Comment or Question

Name
Email
URL (optional)
Title of Comment
Comment
HTML Allowed:
a,code,strong,em
I would like to be e-mailed any response to my comment.

In this section

Related Reading

Related Books

PHP Pocket Reference

Related Ads