JABATAN TEKNOLOGI MAKLUMAT DAN KOMUNIKASI
Objective : 1. Creating Simple Input Form
2. Handling Input From Form With PHP 3. Combining HTML and PHP In Same Page
Equipment : 1. Computer with Windows OS
2. Internet
Task1 : Creating Simple Input Form
HTML FORM and input elements add interactivity to the web site. The HTML forms handle very important operations at the website that is getting the input from user. All the input elements should be enclosed within the opening and closing <form> tag :
<form>
The input elements </form>
Two important attribute for FORM tag is ACTION and METHOD. The ACTION attribute specifies the page which handles the input from the user. Usually, this will be a script which processes the data supplied by user (ex.: displaying in other page, storing in database etc).
The METHOD attribute is either POST or GET that is the way data passed to the page which handles the input mentioned in ACTION. If POST method is used, the information is sent to the server as part of
the data body and will not be visible in the URL box in the user’s browser. If GET method is used, the
input values are passed as part of the URL. If we don’t specify the method, GET is taken as default.
<form action=proses.php method=POST> The input elements
</form>
1. Write the HTML code as follows, save as borang.html and display the output.
<html>
<head><title>Simple Form</title></head> <body>
<form action=proses.php method=POST>
Text : <input type=text name=element1 size=20> <br><br>
JABATAN TEKNOLOGI MAKLUMAT DAN KOMUNIKASI POLITEKNIK SEBERANG PERAI
PSP
Textarea : <br>
<textarea name=element3 rows=3 cols=50 wrap=hard> </textarea> <br><br>
Selection List :
<select name=element4>
<option value=Penang>Penang</option> <option value=Kedah>Kedah</option> <option value=Johor>Johor</option> </select> <br><br>
Radio button : <br>
<input type=radio name=element5 value=male> Male <br>
<input type=radio name=element5 value=female> Female <br><br>
Check box : <br>
<input type=checkbox name=element6 value=PHP> PHP <br> <input type=checkbox name=element6 value=JSP> JSP <br> <input type=checkbox name=element6 value=ASP> ASP <br><br>
Submit : <input type=submit name=element7 value=Submit> <br> Reset : <input type=reset name element8 value=Reset> <br> </form>
</body> </html>
(Note : Review all eight form elements introduced in the HTML document above. Note that for Text, Password and Textarea, input are entered by user, meanwhile for Selection List, Radio Button and Check Box, input are assigned by the programmer using the value attribute.)
Task 2 : Handling Input From Form With PHP
Please keep in mind that HTML part of the forms does not have any feature in processing the data submitted. You should use server-side scripting language such as PHP to handle the data submitted by the forms.
JABATAN TEKNOLOGI MAKLUMAT DAN KOMUNIKASI POLITEKNIK SEBERANG PERAI
PSP
1. Write the PHP script as follows and save as proses.php.
<?
echo “form element 1 value is ”.$element1; echo “form element 2 value is ”.$element2; echo “form element 3 value is ”.$element3; echo “form element 4 value is ”.$element4; echo “form element 5 value is ”.$element5; echo “form element 6 value is ”.$element6;
?>
2. Open the borang.html file in the test server. Fill in the form and submit. Is the value entered displayed in proses.php file ?
Task 3 : Combining HTML and PHP In Same Page
In some circumstances we might want to include PHP script in the same page as a hard-coded HTML form. Such a combination can be useful if we need to present the same form to the user more than once. To achieve this, just point the ACTION attribute in the FORM tag to $_SERVER[PHP_SELF].
1. Write the script as follows and save as borang.php.
<html>
<head><title>Simple Form 2</title></head> <body>
<form action=”<? echo $_SERVER[PHP_SELF] ?>” method=POST>
Num1 : <input type=text name=num1 size=10> <br> Num2 : <input type=text name=num2 size=10> <br>
JABATAN TEKNOLOGI MAKLUMAT DAN KOMUNIKASI POLITEKNIK SEBERANG PERAI
PSP
2. Run and test the script. Is it works ?
Questions :
1. Explain the difference between $_POST, $_GET and $_REQUEST. 2. Write a PHP script to solve the equation given below :
J = π ⅔ j 4
Report :