Using Form
Using forms in a web based application is very common. Most forms are used to gather information like in a signup form, survey / polling, guestbook, etc. A form can have the method set as post or get. When using a form with method="post" you can use $_POST to access the form values. And when the form is using method="get" you can use $_GET to access the values. The $_REQUEST superglobal can be used to to access form values with method="post" and method="get" but it is recommended to use $_POST or $_GET instead so you will know from what method did the values come from. Here is an example of HTML form : Example : form.php
<form method="post" action="<?php
echo $_SERVER['PHP_SELF'];?>"> The form above use $_SERVER['PHP_SELF'] as the action value. It is not required for the form to perform correctly but it's considered good programming practice to use it. Below is the PHP code used to access form values : <?php The if statement is used to check if the send variable is set. If it is set then the form must have been submitted. The script then print the value of username using $_POST and $_REQUEST
Using Array As Form ValuesTake a look at the code example below. The form have five input with the same name, language[]. Using the same input name is common for checkboxes or radio buttons. Example : form-array.php <form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>"> The PHP code below print the value of language after the form is submitted. Go ahead and try the example. Try checking and unchecking the options to see the effect. Example : form-array.php <?php From the above code you will notice that $language is an array. This is because in the form code language is repeated several times. When you specify the same input name in a form, PHP will treat it as an array. There is one important issue you should know when using forms, that is form validation. The code above does not check whether the input is correct such as checking if the user is entering any value into the textbox or is the user selecting any checkboxes. This is actually a bad practice because you should never put a web form without any validation. To learn about validating form input you can go to this page : Form Validation With PHP
That's it. You just completed the basics of PHP programming. When you're ready for more advance programming in PHP check out the book store. You can find plenty good books in there with deeper coverage on PHP programming As is said earlier if you haven't got the PHP manual you should download it now from php.net. You can find lots of interesting (and important) stuff in it. You can get the manual in various format but I recommend that you get the compiled HTML (CHM) version. It's easier to read plus it has search capability and loads of user contributed notes, very useful. Now let's move on the next section : MySQL Tutorial |
![]() |
Using PHP Functions | PHP MySQL Tutorial : Using Forms | Read a Remote File Using PHP |
![]() |
This tutorial is far from perfect so if you have any critiques, questions, comments or problems about this tutorial please tell me. Click here to send your feedback. And if you like this tutorial please link to this site. It will really help a lot :-) |
Copyright © 2004 - 2008 www.php-mysql-tutorial.com | Privacy Policy