<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Getting Started with PHP Programming
By Jeff Cogswell

Rate This Article: Add This Article To:

Getting Started with PHP Programming - Getting Information from the User
( Page 4 of 5 )

Getting Information from the User

At its heart, PHP is a language for web sites. As such, there's a lot of functionality to help you easily create web applications. One basic part of web applications are forms.

PHP makes form-handling easy. You can create two pages, one that contains a form, and one that responds to a form. To show you how, I'll start with the example, and then explain how it works.

Here are two PHP files. The first one contains a web form, and really has no PHP at all. Call this file welcome.php. (I'm giving it a PHP extension anyway, in case you want to add some PHP into it later.)

<html>
<body>
<form action="response.php" method="post">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" />
<input type="submit" value="Submit" />
</form> 
</body>
</html>

And the second one is the response page, which processes what was entered into the form. Call this response.php (its name must match the action attribute of the form tag in the previous, welcome.php, file):

<html>
<body>
<?php
echo ("Hello, {$_REQUEST['fname']} {$_REQUEST['lname']}. Welcome!");
?>
</body>
</html>

The first file is simply an HTML form. The form in this case uses the post method (where the data is sent to the server “quietly” as opposed to being part of the URL), but you're free to replace the word post with get; in that case, you'll see the data in with the address bar of the response page as in response.php?fname=Jeff&lname=Cogswell.

When you're looking at the welcome.php page in your browser and you click submit, the data gets sent to the server, and the the server notes the response.php filename; the server then runs this PHP file, processing it as it would any other PHP file. However, this PHP file can access the data that was entered into the form.

PHP includes several predefined variables, which are variables that are automatically filled with information. One such predefined variable is the $_REQUEST variable, which is how you access that form data. (The predefined variables all start with an underscore, and are in all caps. You can see the list of predefined variables here: http://www.php.net/manual/en/reserved.variables.php.)

In this case, you can access the form data using these two variables:

$_REQUEST['fname']

and

$_REQUEST['lname']

And notice how I used them in a string with double quotes:

echo ("Hello, {$_REQUEST['fname']} {$_REQUEST['lname']}. Welcome!");

As I described earlier, I enclosed the variable names in curly braces, although technically I could probably get away without doing that this time. These variables get replaced by their actual values—the values typed into the form on the previous page—and, thanks to the echo statement, are printed out on this page.

Here the end result. Figure 4 shows the form itself in the first page, welcome.php.

Figure 4

After entering the information, I pressed Submit, and the new page, response.php, opens up, showing me the information I typed in.

Figure 5

Of course, the goal usually isn't to just spit the text back to the user. The key here is that when the code in the second page, response.php, is running, it has the user's submitted data. From there the PHP code could, for example, enter the information into a database, or do whatever processing is needed. That's a more advanced topic we'll cover in a future article.

Functions Galore

One of the features that makes PHP so useful is the huge number of functions available. In this little article I certainly can't list them all, but I can point you to where you can learn more. (And in future articles in this series, I'll be explaining a lot of them.) The official documentation describes all the built-in functions, which includes database control, mathematical functions, image processing (so you can generate images for the web page), session and authentication (so your users can log into your web site), text processing, and much more. Here's where you can find the list of function libraries; the list is organized by category:

http://www.php.net/manual/en/funcref.php



 
 
>>> More Microsoft Languages Articles          >>> More By Jeff Cogswell