Download presentation
1
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools
2
Two-tier Software Architecture
2. Server retrieves PHP page from storage Browser sends URL to request PHP page Web server - compiles PHP code client - starts program 3. Sends results to client replaces code with code outputs 4. Browser displays page
3
Example
4
Exercise – simple calculation (arithmetic, for-loop)
makes HTML wrapper 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.
5
Requests PHP execution
Demonstrate HTML Form requests simple calculation m1.html 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
6
Architecture Overview for Example
program requested data for program query-string submits Form m1.php ?NAME=Joe &M=2&N=2 retrieves m1.php compiles m1.php starts program m1.html web.njit.edu 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”)
7
Example Code Overview
8
HTML FORM LAYOUT Exercise – next 3 slides PHP program requested Form data/variables <form > </form > action="m1.php" <input /> NAME <br> type="text" name="NAME" <input type="text" name="M" /> M <br> <input type="text" name="N" /> N <br> <input type="submit" value ="Press" /> activates Form: sends data to server asks server to execute m1.php
9
PHP File Layout fileName.php Initial HTML <?php PHP statements here ; ?> More HTML <?php PHP statements here ; ?> More HTML
10
PHP code for calculations
<html><head> <style type="text/css"> body { background-color: cyan ; font-size:18pt ; } </style> </head><body> PHP code for calculations HTML Form variable names Code sends products 1*1 .. M * N to browser <?php ?> Gets data M & N from server $M = $_GET[ "M" ]; $N = $_GET[ "N" ]; PHP variables begin with $ print "Here come the multiples!<br><br>" ; Sends output stream to browser for ( $i=1 ; $i <= $M; $i++ ) { print "<br>" ; }; PHP variable names Typical for-loop for ( $k=1 ; $k <= $N; $k++ ) { $x = $i * $k ; print "$i * $k = $x" ; print "<br>" ; } Nested for-loop See with: View > Page Source Generates dynamic HTML Intermingled HTML and text. print "That's all folks<br><br>" ; Displays PHP code. show_source("m1.php"); </body></html>
11
PHP Syntax checking Usually white screen of death signals PHP syntax error.
12
More In-class Exercises
Only actual Assignments have to be handed in.
13
Exercise-1: Generate random password and email to user.
1. Make HTML Form -- with name and address fields. 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 address $to = $subject = "urgent"; $message = "$p" ; $from = Sent to: preferred: p.195 DON’T mail me. PHP built-in functions mail ( $to, $subject, $message, "From: $from" ) or print "Could not send mail.“ ;
14
Exercise-2: Insert Form data in database table Information via PHP.
Make HTML Form to send: name address password to PHP script. See Text pages for PHP -- also notes for How to do! 2. Script should: Get data. Insert name, password, address in MySQL table Information
15
Homework Assignments Preliminary Remarks
16
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 ; Echo to browser. Assignment 03 Database Retrieval Make HTML Form that submits password and sends HTML & s copy of diary entries for password. Script should: Use password to retrieve diary data from Diary table and retrieve address & name from Information table. Send HTML version of results to browser wrapped in HTML table. text version.
17
Assignment 02 Database Insert – some references.
HTML Form to send name password diary or log entry address to PHP script. Text pages 217 Text page 178 for date function See Text Pages Script should: Get Form data (including Diary entry from an HTML Textarea) Text pages 194 for mail function Connect to database 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 for confirmation.
18
Assignment 02 - Connect to database - execute SQL insert
Text page 217 Assignment 02 - Connect to database - execute SQL insert Fill in as needed $username = "---" ; $password = "---" ; $hostname = "sql.njit.edu"; $project = "---" ; 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( ) );
19
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– function Input Form– MySQL insert Input Form– html wrap table - assignment Syntax checking -
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.