PHP:Hypertext Preprocessor
PHP:Hypertext Preprocessor
Preprocessor
What is PHP?
Why PHP?
PHP runs on different platforms
Working of PHP.
</body>
</html>
Variables in PHP
PHP Function
<?php
// This is a function
function foo($arg_1, $arg_2)
{
$arg_2 = $arg_1 * $arg_2;
return $arg_2;
}
PHP strings
Concatenation
Hello PHP
$datedisplay=date(yyyy/m/d);
Print $datedisplay;
# If the date is April 1st, 2009
# It would display as 2009/4/1
$datedisplay=date(l, F m, Y);
Print $datedisplay;
# If the date is April 1st, 2009
# Wednesday, April 1, 2009
PHP Include
Include opendb.php;
Include closedb.php;
This inserts files; the code in files will be inserted into current
code. This will provide useful and protective means once you
connect to a database, as well as for other repeated
functions.
Include (footer.php);
The file footer.php might look like:
<hr SIZE=11 NOSHADE WIDTH=100%>
<i>Copyright 2008-2010 KIIT </i></font><br>
<i>ALL RIGHTS RESERVED</i></font><br>
<i>URL: http://www.kiit.edu</i></font><br>
PHP-Forms
second.php
showtable.php
second.php
<html><head><title>MySQL Table Viewer</title></head><body>
<?php
// change the value of $dbuser and $dbpass to your username and password
$dbhost = 'hercules.cs.edu:3306';
$dbuser = 'nruan';
$dbpass = *****************;
$dbname = $dbuser;
$table = 'account';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db($dbname))
die("Can't select database");
showtable.php
<html><head>
<title>MySQL Table Viewer</title>
</head>
<body>
<?php
$dbhost = 'hercules.cs.edu:3306';
$dbuser = 'nruan';
$dbpass = **********;
$dbname = 'nruan';
$table = $_POST[table];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());
if (!mysql_select_db($dbname))
die("Can't select database");
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) die("Query to show fields from table failed!" . mysql_error());
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++) {
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
mysql_close($conn);
?>
</body></html>
Functions Covered
mysql_connect()
mysql_select_db()
include()
mysql_query()
mysql_num_rows()
mysql_fetch_array()
mysql_close()
PHP-Sessions
Save it as session.php
<?php
session_start();
if (!$_SESSION["count"])
$_SESSION["count"] = 0;
if ($_GET["count"] == "yes")
$_SESSION["count"] = $_SESSION["count"] + 1;
echo "<h1>".$_SESSION["count"]."</h1>";
?>
<a href="session.php?count=yes">Click here to count</a>
What is Ajax?
Client/Server Apps:
Dynamic data
Static forms, controls, code, etc.
Efficient, but not flexible
Ajax Apps:
Dynamic data
Static or dynamic forms, controls, code, etc.
Best of both worlds
22
23
XMLHttpRequest Methods
send (params)
Sends request including postable string or DOM object data
abort ()
Terminates current request
getAllResponseHeaders ()
Returns headers (name/value pairs) as a string
getResponseHeader (header)
Returns value of a given header
setRequestHeader (label,value)
Sets Request Headers before sending
1/25/2006
24
XMLHttpRequest Properties
onreadystatechange
Event handler (your code) that fires at each state change
readyState
0 = uninitialized
1 = loading
2 = loaded
status
HTTP Status returned from server: 200-299 = OK
responseText
String version of data returned from server
responseXML
XML DOM document of data returned
statusText
Status text returned from server
25
Simple Example
var req = new XMLHttpRequest();
req.onreadystatechange = myHandler;
req.open("GET", "servlet", true);
req.send("p1=abc");
...
function myHandler() {
if (req.readyState == 4) {
doSomethingWith(req.responseXML);
}
else if (req.readyState == 3) {
showProgressIndicator();
}
}
26
AJAX-Implementation
BACK THEN
Click
Search