390 likes | 726 Vues
Form & PHP. Introduction. All of the following examples in this section will require two web pages. The first page retrieves information posted by the user, and the second sends the information from the web server and scripting engine back to the browser.
E N D
Introduction • All of the following examples in this section will require two web pages. • The first page retrieves information posted by the user, and the second sends the information from the web server and scripting engine back to the browser. • The first web page doesn’t have to contain any PHP script at all. • Second page contains PHP codes embedded in HTML codes.
Getting Data from Form • An HTML form is a section of a document containing normal content, markup, special elements called controls (text field, checkboxes, radio buttons, drop down menu, text area), and labels on those controls. • Users generally "complete" a form by modifying its controls (entering text, selecting menu items, etc.), before submitting the form to an agent for processing (e.g., to a Web server, to a mail server, etc.)
Getting Data from Form • To create a form using a pure HTML web page: Opening and closing <form> tags. (< form >……< /form> ) • Any controls such as text field , radio button etc that are placed between the <form> tags, automatically become part of the form that is sent to the web server.
Getting Data from Form • Method action tells the server which page to go to once the user has checked a submit button on the form • Example: <form method= post action =“test.php”> ……………. </form>
Getting Data from Form • Attribute method controls the way that information is sent to the server • There are two values for attribute method: get and post • Example: <form method= post action = “test.php”> or <form method= get action =“test.php”> • In this course, most of our examples use post.
Getting Data from Form Text Fields (text boxes) • Text fields are created using <input> element • Setting type attribute to text
Getting Data from Form textfield.html <html> <head></head> <body> <form method = post action = “textfield.php”> Enter your name here : <input name = “name” type=“text”> <br><br> <input type = submit > </form></body></html> textfield.php <html> <head></head> <body> My name is : <?php echo $name ; ?> </html>
Getting Data from Form textfield.html textfield.php My name is hani
Getting Data from Form Text area • use < textarea> tag instead of <input> tags because <textarea> allow us to set the size and number of rows and columns of the control.
Getting Data from Form textarea.html <html> <head></head> <body> <form method=post action="textarea.php"> <p>what are your favourite html editor? <textarea name="editor" rows="5" cols="50" > ultra edit visul interdev note pad crimson editor </textarea> <br> <br> <input type="submit"> </p></form></body></html>
Getting Data from Form textarea.php <html> <head></head> <body> My favourite HTML editor are: <? php echo $editor ; ?> </body> </html>
Getting Data from Form textarea.html textarea.php My favourite HTML editor are :Ultra Edit Visual Interdev Note Pad Crimson Editor
Getting Data from Form Checkboxes • Checkbox is counted as an individual entity. • Users can have several checkboxes ticked altogether, or none checked at all. • Checkboxes are created using <input> tag, setting type attribute to checkbox. • Each checkbox controls was named and set independently.
Getting Data from Form checkboxes.html <html><head></head> <body> <form method=post action “checkboxes.php”> have you ever use sybase before? <input name="checkbox1" type="checkbox" value=“Sybase"> <br>Have you ever use oracle before? <input name="checkbox2" type="checkbox" value=“Oracle"> <br>Have you ever use sql server before? <input name="checkbox3" type="checkbox" value=“SQLServer"> <br><br> <input type="submit" > </form> </body> </html>
Getting Data from Form checkboxes.php <html> <head></head> <body> <?php echo “$checkbox1<br>”; echo “$checkbox2<br>”; echo “$checkbox3<br>”; ?> </body> </html>
Getting Data from Form checkboxes.html checkboxes.php Oracle SQLServer
Getting Data from Form Radio buttons • Only one of the options / answers can be selected at a time. • Radio buttons are created using <input> tag , setting type attribute to radio. • Each radio buttons has a same name.
Getting Data from Form radiobutton.html <html><head></head><body> <form method=post action="radiobutton.php"> c language,object oriented(java) .....can be considered as: <br><br> <input type="radio" name="radiobutton" value=“Networking"> networking <br> <input type="radio" name="radiobutton" value=“Programming Language" checked> programming language <br> <input type="radio" name="radiobutton" value=“Operating System"> operating system <br><input type=submit ></form></body></html>
Getting Data from Form radiobutton.php <html> <head></head> <body> <? php echo “you selected the answer :$radiobutton”; ?> </body> </html>
Getting Data from Form radiobutton.html radiobutton.php You selected the answer : Programming Language
Getting Data from Form Dropdown List boxes • Dropdown list boxes are created using <select> and <option> tags. • The <select> tag that creates the list box encloses a number of <option> tags. the <option> tags each contain the text that corresponds to an item on the dropdown list.
Getting Data from Form listboxes.html <html><head></head><body> <form method=post action="listbox.php"> what price of laptop are you looking to buy ? <br> <br> <select name="price"> <option selected>RM 3000- RM 3500</option> <option>RM 3500-RM 4000</option> <option>RM 4000-RM 4500</option> </select> <br><br> <input type=submit> </form></body></html>
Getting Data from Form listbox.php <html> <head></head> <body> <?php echo “Price Range : $price" ; ?> </body> </html>
Getting Data from Form listboxes.html listbox.php Price Range: RM 3000- RM 3500
Inserting Data from Form Into Database • The next topic shows how to insert data from form into MySQL database. • The form has all type of form controls and the complete codes are shown in form_complete.html. • form_complete.html contains all HTML tags. • form_dbase.php contains PHP codes that send the data from form into MySQL database.
Inserting Data from Form Into Database form_complete.html
Inserting Data from Form Into Database form_dbase.php
Inserting Data from Form Into Database form_complete.html
Inserting Data from Form Into Database Database Table Database content