Php basic tutorial-2 create templates, process forms, basic tutorial forms
1. Create a template:
Copy the frequently-seen part of the page to an html or PHP file, and introduce it using the require ()/include () function in the original page.
Example:
Source html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Copied header: header.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Copy tail: footer.html:
<div><p>This is the foot of the document</p></div></body>
Merging template: ws. php
<?phprequire('header.html');?><form action="handle.php" method="post"> <p> Name:<select name="title"> <option value="Mr">Mr</option> </select> <input type="text" name="name" size="20"/></p> <input type="submit" value="send"></form><?phprequire('footer.html');?>
2. process forms-display and process forms on a page at the same time
Conditional statement:
If (Form submission) {Processing Form}
Else {display form}
Example: Simple user name-password verification
Enter: User name: YF password: 123456
Display: Login successful
Cut chart:
Code: ws.php (header.html?footer.html references the template above ):
<?phpdefine('TITLE', 'Login');require('header.html');if (isset($_POST['submitted'])) {if ((!empty($_POST['name'])) && (!empty($_POST['password']))){if ((strtolower($_POST['name']) == 'yf') && ($_POST['password'] == '123456')){// name and password are correct.print '<p> logged in !</p>';}else {print '<p> name or password is worry!</p>';}}else {print '<p> make sure you enter both name and password!</p>';}}else {print '<form action="ws.php" method="post"> <p> Name:<input type="text" name="name" size="20"/></p> <p>Password:<input type="password" name="password" "size="20" /></p> <input type="submit" value="send"> <input type="hidden" name="submitted" value="true"/></form>';}require('footer.html');?>