If you want something you've never had, you must be willing to do something you've never done!

Powered by Blogger.

Php Programming Tutorial "Forms" 1

Cerita Semalam Aaz | 10:42 PM | ,

The HTML <form> tag will let the user fill in the necessary information and pass it in a query string to another or to the same page. Here is a simple HTML form:
<html>
<body bgcolor="lime">
<form method="GET">
<p>What are your preferences?</p>
<p>
Font color: <input type="text" name="font-color"></p>
<p>
Background Color: <input type="text" name="background-color"></p>
<input type="submit" value="Submit this form">
</form>
</body>
</html>
I saved this page as preferences.html. When you call this page and submit it you will notice that the form sent the values you filled as pairs in the query string.
The <input> tags with the type attribute set to "text" will hold the values that the user fills and that one with the type attribute set to "submit" will create a button that when clicked will submit the form. The <p> tag is just to create paragraph spacing.
Now we can use this form to build a nice interactive Web experience. We will catch the values sent by the form in the query string and use it in the page. preferences.php:
<html>
<body style="background-color:<?php echo $_GET['background-color']; ?>">
<form method="GET" style="color:<?php echo $_GET['font-color']; ?>">

<p>What are your preferences?</p>

<p>
Font color:
<input type="text" name="font-color"
value="<?php echo $_GET['font-color']; ?>"></p>

<p>
Background Color: 
<input type="text" name="background-color"
value="<?php echo $_GET['background-color']; ?>"></p>

<input type="submit" value="Submit this form">

</form>
</body>
</html>
Call it with the query string attached:
http://localhost/preferences.php?font-color=red&background-color=yellow
Now enter and submit colors at your will. Most of the HTLM tags accept the style attribute in which we can set diverse CSS properties, including colors. We just fill the values of those properties with the values received by PHP and available in the $_GET array. Take a look at the page source to see how are the properitie's values replaced.

1 Responses So Far:

Related Post

 
Back To Top