Download presentation
Presentation is loading. Please wait.
1
Web Technology HTML and PHP
2
Create Form in HTML Action:
<form name=‘myform’ action=‘filename.php’ method=POST> …Form’s Objects….. ……………………….. </form> Action: Specify the target php file to process this form method: GET: send data by appending them to the URL POST: send data with the body of form Data not visible to user
3
Set Variable Name to Form’s Objects
Text Select Radio Button (use same names for all in the group) Check Box (use array[] for all in the group) <input type=‘text’ name=‘txtName’ value=‘’> <select name=‘sltDept’ > <input type=‘radio’ name=‘rdGender’ value=‘M’>Male <input type=‘radio’ name=‘rdGender’ value=‘F’>Female <input type=‘checkbox’ name=‘chkHobby[]’ value=‘sport’>Sports <input type=‘checkbox’ name=‘chkHobby[]’ value=‘book’>Books <input type=‘checkbox’ name=‘chkHobby[]’ value=‘music’>Music
4
Example of a Form <form action="test.php" method="post">
<input type="text" name="txtName"> <input type="radio" name="rdDept" value="TE">TE <input type="radio" name="rdDept" value="TM">TM <input type="radio" name="rdDept" value="TCT">TCT <input type="radio" name="rdDept" value="TTC">TTC <select name="sltDegree"> <option value="">=== Highest Degree ===</option> <option value="B">Bachalor</option> <option value="M">Master</option> <option value="D">Doctoral</option> </select> <input type="checkbox" name="chkHobby[]" value="music">Music <input type="checkbox" name="chkHobby[]" value="sport">Sports <input type="checkbox" name="chkHobby[]" value="book">Books </form>
5
Example of the Form
6
Retrieving Values from the Form in PHP
If the method is “GET” If the method is “POST” We can use $_REQUEST[‘varname’] to get value, regardless of the method used in the form Let’s try this “phpinfo();”; <? echo $_GET[‘varname’]; ?> <? echo $_POST[‘varname’]; ?>
7
Retrieving Values from the Form in PHP
Retrieving Values from a Text, Radio Button, and select is easy. Just refer to the object’s name (variable name) we want to retrieve the value from <? echo $_POST[‘txtName’].”<BR>”; echo $_POST[‘rdDept’].”<BR>”; echo $_POST[‘sltDegree’].”<BR>”; ?>
8
Retrieving Values from the Form in PHP
Retrieving values from checkbox is a bit tricky We never know how many items would be checked It is also possible that no items be selected at all <?php for($i=0; $i < sizeof($_POST['chkHobby']); $i++) echo $_POST['chkHobby'][$i]."<HR>"; ?> or $hobby = $_POST[‘chkHobby’]; for($i=0; $i < sizeof($hobby); $i++) echo $hobby[$i]."<HR>";
9
So! What else? Make sure we validate data before submitting
Use javascript to check the validity of data before sending them to the target PHP This is to make data ready for processing in PHP How can we make the form look nicer? Use <table> or <style> to help display better output What happens if method is “GET”, instead? Try to play with $_REQUEST[‘xxx’], see what happens? What happens if we want to store these values in the database?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.