Anda di halaman 1dari 5

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 users browser. If GET method is used, the input values are passed as part of the URL. If we dont 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> Password : <input type=password name=element2 size=20> <br><br> 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>
1
Dr. Ahmad Zamzuri Bin Mohamad Ali

Submit : <input type=submit name=element7 value=Submit> <br> Reset : <input type=reset name element8 value=Reset> <br> </form> </body> </html>

Figure 7.1.1: Output for the simple form (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. The PHP $_POST or $_GET variable are used in collecting the data from form. If the data is sent using POST method, $_POST variable is used, meanwhile if the data is sent using GET method, $_GET variable is used. $_REQUEST variable can be used to get the data sent with both GET and POST methods.

Dr. Ahmad Zamzuri Bin Mohamad Ali

1. Write the PHP script as follows and save as proses.php. <?php //Get the input from form $ element1 = $_POST[element1 ]; $ element2 = $_POST[element2 ]; $ element3 = $_POST[element3 ]; $ element4 = $_POST[element4 ]; $ element5 = $_POST[element5 ]; $ element6 = $_POST[element6 ]; //Display the output 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 ?

Figure 7.2.1: Output when you use server-side scripting language such as PHP to handle the data submitted by the forms.

Task 3 : Combining HTML and PHP In Same Page


3
Dr. Ahmad Zamzuri Bin Mohamad Ali

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 4oring.php. <html> <head><title>Simple Form 2</title></head> <body> <form action=<?php echo $_SERVER[PHP_SELF] ?> method=POST> Num1 : <input type=text name=num1 size=10> <br> Num2 : <input type=text name=num2 size=10> <br> <input type=submit value=Submit> <input type=reset value=Reset> <br> </form> </body> </html> <?php //Get the input $num1= $_POST[num1]; $num2= $_POST[num2]; //Process $sum = $num1 + $num2; //Display the output echo The answer is .$sum; ?>

7.3.1: Output that display Simple Form 2

2. Run and test the script. Is it works ?

Dr. Ahmad Zamzuri Bin Mohamad Ali

Figure 7.3.2: Output Combining HTML and PHP In Same Page Questions : 1. Explain the difference between $_POST, $_GET and $_REQUEST. 2. Write a PHP script to solve the equation given below : J=j4 Report : 1. Write the report with results, discussion and appropriate conclusion.

Dr. Ahmad Zamzuri Bin Mohamad Ali

Anda mungkin juga menyukai