Php tutorial - part 2

In the previous tutorial I explained how to include php code in a file and how to print text to the browser window. In this installment I'll describe how to do something a little more useful.

This tutorial assumes that you have at least a basic knowledge of html and html forms. If you do not, go here and read some tutorials on the subjects.

Open the text editor of your choice( notepad or vi (http://www.vim.org/) will do fine if it's all you have).


Copy and paste the following into it:
--- start copy below here ----

<html>
<head>
<title>
Choose a color
</title>
</head>
<body>
<form action="handle_form.php" method="post">
What is your favorite color?
<br>
<input type="text" name="color_chosen">
<br>
<input type="submit" name="submit_button" value="Submit">
</form>
</body>
</html>

----- end copy above here ------

Now save this file as "html_form.php" in the web directory of your web server(we talked about this briefly in the previous tutorial). This directory will probably either be named "htdocs" or "www".

--- start copy below here ----

<html>
<head>
<title>
The color you chose
</title>
</head>
<body>
Yeah, I like <?php echo $_POST['color_choice']; ?> too.
</body>
</html>

----- end copy above here ------

Ok, copy and paste the above code into your text editor and name it "handle_form.php" . Put it in the same directory as the previous file you created.
Now go to "http://localhost/html_form.php" in your browser and fill the form out, and hit the submit button.
You should get a message in your browser like: "Yeah, I like blue too."
The color in the message will be whatever you submitted to the form.

Ok, now let's break down what each of these files do.
html_form.php

I'm not going to explain to much about what is going on in "html_form.php" since this is not an html tutorial. If you do not understand all of what is in that file you should go here and read up on html.

There are however two lines that I will describe in this file.


<form action="handle_form.php" method="post">

This is the line that starts the html form. The two most important parts of this html tag are the the "action" and "method" attributes.
There are only two attributes in the form tag used in this example, but there can be more in a form tag.
But every form tag should always have these if you plan on processing any of the submitted information with a php script.

The value of the "action" attribute should be the name of the php script that you want to process the form data.
If the php script that is processing the form is in another directory you will have to supply the path to the file.

Example:

<form action="http://mysite.com/anotherDirectory/handle_form.php" method="post">

For now you should put both files in the same directory because we are trying to make this as least complicated as possible.

Also for now, let's just say that the value for the "method" attribute should always be "post".


Another thing to point out about this file is that it is a simple html file with no php in it, yet when we named it we gave it a .php extension.
You didn't have to, you could call it "html_form.html" if you wanted.
But if you plan on doing a lot with php on your site, it can't hurt to give all your html files .php extensions when you create them.
And some time down the road when you decide you want to add some php code to that html document, you won't have to worry about changing all the links to "html_form.php" instead of "html_form.html"!
handle_form.php

asdf
Next we'll discuss how to use MySQL with php... check back soon!