Transcript PHP 01

PHP Scripts
HTML Forms
Two-tier Software Architecture
PHP Tools
Two-tier Software Architecture
1. Browser sends
URL to request
PHP page
client
2. Server retrieves PHP page
from storage
Web
server
- compiles PHP code
- starts program
3. Sends results
to client
4. Browser
displays page
-replaces code with
code outputs
Example
Exercise – simple calculation (arithmetic, for-loop)
makes HTML wrapper
1. Make HTML Form
that sends name and integers M and N
to PHP script.
2. PHP Script:
Gets Form data.
Sends multiples up to M * N, to browser on successive lines.
Demonstrate
m1.html
HTML Form requests simple calculation
HTML Form
Sends data
Requests PHP execution
m1.php
PHP program
Gets data from Server
Calculation
HTML wrapper
Posted under Assgt02 directory
Test with m1.html and data
Test with m1.php directly
Architecture Overview for Example
program
data for
program
submits requested query-string
Form
m1.php ?NAME=Joe &M=2&N=2
retrieves m1.php
compiles m1.php
starts program
m1.html
results to client
data
Gets Name, M, N values
Calculates based on
input and sends results
to browser
Browser
renders page
show_source (“m1.php”)
Example Code Overview
Exercise – next 3 slides
HTML FORM
LAYOUT
PHP program requested
Form data/variables
<form
action="m1.php"
<input
>
type="text" name="NAME"
/> NAME <br>
<input type="text" name="M"
/> M <br>
<input type="text" name="N"
/> N <br>
<input
type="submit"
value ="Press" />
</form >
activates Form: sends data to server
asks server to execute m1.php
PHP File
Layout
fileName.php
Initial HTML
<?php
PHP statements here ;
?>
More HTML
<?php
PHP statements here ;
?>
More HTML
<html><head>
<style type="text/css">
body { background-color: cyan ; font-size:18pt ; }
</style>
</head><body>
PHP code for calculations
<?php
$M = $_GET[ "M" ];
$N = $_GET[ "N" ];
HTML
Form variable
names
print "Here come the multiples!<br><br>" ;
PHP for ( $i=1 ; $i <= $M; $i++ )
{ names
print "<br>" ;
variable
for ( $k=1 ; $k <= $N; $k++ )
{ $x = $i * $k
;
print "$i * $k = $x" ;
print "<br>"
;
}
};
print "That's all folks<br><br>" ;
show_source("m1.php");
?>
</body></html>
Code sends products
1*1
M * NM & N from server
Gets.. data
to browser
PHP variables begin with $
Sends output stream
to browser
Typical for-loop
Nested for-loop
See
with: dynamic HTML
Generates
View > Page Source
Intermingled
HTML and text.
Displays PHP code.
PHP Syntax checking
www.meandeviation.com/tutorials/learnphp/php-syntax-check/
Usually white screen of death signals PHP syntax error.
More In-class Exercises
Only actual Assignments have
to be handed in.
Exercise-1: Generate random password and email to user.
1. Make HTML Form -- with name and email address fields.
http://us2.php.net/manual/en/
2. PHP script should:
Get the Form data.
Generate random string for password
$p =crypt ( "password“ ) generates new random string at each call
Mail password to your email address
$to
$subject
$message
$from
PHP built-in
functions
=
=
=
=
"[email protected]";
"urgent";
"$p" ;
"[email protected]";
mail ( $to, $subject, $message, "From: $from" )
or print "Could not send mail.“ ;
p.195
DON’T
mail me.
Exercise-2: Insert Form data in database table Information via PHP.
1. Make HTML Form to send:
name
email address
password
to PHP script.
2. Script should:
Get data.
Insert name, password, email address
in MySQL table
Information
See Text pages 213-218
for PHP
-- also notes for How to do!
Homework Assignments
Preliminary Remarks
Assignment 02 Database Insert
Make HTML Form that sends
name, password, diary or log entry to PHP script.
Script should:
Get Form data (including diary entry from Textarea)
Create date
Insert diary text, date, password in MySQL table Diary.
Mail transaction to your email; Echo to browser.
Assignment 03 Database Retrieval
Make HTML Form that submits password
and sends HTML & emails copy of diary entries for password.
Script should:
Use password to retrieve diary data
and retrieve email address & name
-- from Diary table
-- from Information table.
Send HTML version of results to browser wrapped in HTML table.
Email text version.
Assignment 02 Database Insert – some references.
HTML Form to send
name
password
diary or log entry
email address
to PHP script.
Text pages 217
Text page 178
for date function
Script should:
See Text
Pages 217-218
Get Form data (including Diary entry from an HTML Textarea)
Connect to database
Text pages 194
for mail function
Create date
Insert date, password, diary text into MySQL table Diary.
Echo SQL and input from Form in clear manner.
Mail input data to your email for confirmation.
Assignment 02 - Connect to database - execute SQL insert
Text
page 217
Fill in as needed
$username
$password
$hostname
$project =
=
=
=
"---"
"---"
;
"---"
;
"sql.njit.edu";
;
Students use sql2
Connects or exits
mysql_connect ($hostname, $username, $password)
or die ("Unable to connect to MySQL");
echo "Connected to MySQL<br> ";
mysql_select_db ( $project ) or die ( mysql_error ( ) ) ;
$s = "insert into name-of-table values ( value-1 , value 2, value-3 ) " ;
print "Connected to MySQL<br> ";
Execute SQL insert
mysql_query ( $s ) or die ( mysql_error( ) );
Concepts
Client server diagram
HTML Form elements
Query string
Two tier architecture
Trivial program – no Form, run PHP code
Input Form– get & echo
Input Form– email function
Input Form– MySQL insert
Input Form– html wrap table - assignment
Syntax checking - http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/