HTML Basic web content Present static data Forms


� HTML �Basic web content �Present “static” data �Forms � Java. Script �Ability Limited ability to interact with other open pages �Can to interact with current web page read, process and modify data on local page Validate data �Can initiate another program through submitting a form

� “Container” �Groups data entered on a form � Has a mechanism to pass the data to another program �Submit �Action=“program. name” �Method GET � Data sent as part of URL POST � Data sent “under the covers” � Part of the http transfer � Target program is on a Web server

� Software � XAMPP Development Environment OS � Linux (LAMPP) � Windows (WAMPP) Apache Web Server � Serves the web pages My. SQL � Serves as the database PHP � Scripting language on the server � Configuration � Server processes the php code on the server side Before sending back to the client

� http: //sis-43 spp 11 -pc/grades. htm � Simple set of Web programs to: �Display Students �Display Student Grades �Enter Student’s Grades �Add Students

� Form (not used) � Anchors � 2 links to html programs to gather more information � 2 links to php programs to perform data retrieval and return a customized Web page

Grades. htm Display_Students. php Select_Student_For_Grade. php Enter_Grade_Student. htm Add_Student_Form. htm Display. Student. Grades. php Under development add. Student. php

Display_Students. php

<html> <head><title>Display Students</title></head> <body> <h 1>List of Students</h 1><hr> <table dir="ltr" border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td valign="top" width="1%“> </td> <td valign="top" width="24"></td> <td valign="top"> <? php $server = 'localhost‘; $user = 'root‘; $pass = 'xyz'; $mydb = 'gradesdb'; $connect = mysql_connect($server, $user, $pass); if (!$connect){ die ("Cannot connect to $server using $user"); } else { $sqlcommand = "select * from students"; $con_results = mysql_select_db ($mydb); if (!$con_results) { print "con results: $con_resultsn“; die ("Could not connect to db"); } else { print " The query is: $sqlcommand "; $results_id = mysql_query($sqlcommand, $connect); if (!$results_id) { // did it get the data print "results_id: $results_id n“; die ("Could not select students from db"); // no! } else { // got it! print "<p>Students in database</p>n"; print "<table border='1'>n"; print "<tr>n"; print " <th>ID</th>n“; print " <th>First Name</th>n“; print " <th>Last Name</th>n"; print "</tr>n"; while ($row = mysql_fetch_row($results_id)) { print "<tr>n"; foreach ($row as $field) // note the foreach! { print " <td>$field</td>n“; } print "</tr>n"; } // end while print "</table>n"; } // end if. . else. . results_id } // end if. . else. . con_results } // end if. . else. . connect ? > <p><a href="grades. htm">Return to Main</a></p> </td> </tr> </table> <hr> </body> </html>

� Contains a form �Action = “add. Student. php” �When submitted does the action Creates an entry in the database Returns a web page confirming action

add. Student. php

<html> <head> <title> Add Student </title> </head> <body> <? php $server = 'localhost'; $user = 'root'; $pass = 'xyz'; $mydb = 'gradesdb'; $connect = mysql_connect($server, $user, $pass); if (!$connect) // check if connect to DB worked {die ("Cannot connect to $server using $user"); } else { // we’re in! print "connected n"; $table = "students"; $first. Name = $_POST['f. Name']; // get the data passed through POST $last. Name = $_POST['l. Name']; $sqlcommand = "insert into $table (fname, lname) values ('$first. Name', '$last. Name')"; $con_results = mysql_select_db ($mydb); if (!$con_results) // check if connect to database { print "con results: $con_resultsn“; die ("Could not connect to db"); } // problem! else { // connected, now try to save data print " The query is: $sqlcommand "; $results_id = mysql_query($sqlcommand, $connect); if (!$results_id) // able to add data ? ? { print "results_id: $results_id n“; print 'Could not add students to db, error: '. mysql_error(); } // nope! else // success! { print "Students added to db“; } // end results_id } // end if con_results } // end if connnect print 'Done'; ? > <hr> <p><a href="Add_Student_Form. htm">Return to Add Students</a></p> <p><a href="grades. htm">Return to Main</a></p> </body> </html>
- Slides: 12