Creating Databases for Web applications Server side vs client side PHP basics Homework: Get your own versions of sending working: both html and Flash!
PHP php files are interpreted by/on/at the server. One or more of the following happens: –php interface to operating system is used to do things such as interact with databases OR files OR –HTML is produced (and sent to the client) –cookie is set and/or accessed
Structure CLIENT Browser (Firefox): Requests URL SERVER PHP interpreter SERVER DBMS (MySQL) Build HTML page
Mechanics in php script Need to distinguish html versus php Common task will be to generate a string (aka character string) made up of html you know ahead of time and html generated from variables.
phpinfo script PHP info test <? echo phpinfo(); ?>
Variables Concept in all computing Variable 'is' a name that holds a value. Can be used in place of a literal value. The value can vary over use PHP: variable names begin with $, no spaces, avoid punctuation except for _ Rule: long enough for you to remember what it is used for and short enough to type.
php example Test Welcome to the store <?php …. Print(" New Products "); Print( " "+$newproduct1name); ?> Variable set & used
php variables within strings For all php variables, you can put the variable name in a string: print("The name input was: $fname"); –php figures out that $fname is a variable and gets its value. NOTE: out of habit in using other programming languages, sometimes I don't do that: print ("The name input was :". $fname); NOTE: the string concatenation operator is. Caution: SQL often requires a single quotation mark
Form data in php Built-in functions $_GET[' '] $_PUT[' '] If you want to NOT distinguish (considered less secure) $_REQUEST[' '] Name in form
Checking if a form variable has been set Common situation to check if this is first time Prevents error if no value exists
random rand (a, b) returns a pseudo-random choice from a to b, inclusive $choice=rand(0, sizeOf($states)-1); The arrays in php (like JavaScript and ActionScript) start indexing at zero.
Overview Useful to think in terms of 3 language domains (my terminology): –client side browser (interpreter of html) –server side interpreter of asp/JavaScript or php (There is another level here for the operating system, but this is what the asp objects and the php built-in functions & variables provide.) –database interpreter of SQL (by Access or MySQL)
Warnings Applicable to php and other languages: To output quotation marks in the HTML, you may use single quotes or 'escape' double quotation marks: print (" "); Response.Write(" "); SQL statements require quotation marks around values in certain cases. SQL statements use a single equals sign (=) for checking for equality. asp/JavaScript and php use == in IF statements.
php file for sending Note: this file can be used from HTML and from Flash / ActionScript Must execute on server Could use post or get [or $_REQUEST] This is what is used in Catching Bo
send .php <?php $to = $_POST['to']; $subject = $_POST['subject']; $body = $_POST['body']; $headers = "From: ". $_POST['from']; if (mail($to, $subject, $body,$headers)) { echo("Your message was sent"); } else { echo("There was a problem."); } /* message is send even if (s) not good! */ ?>
Extra credit possibility Figure out how to get an error Note: the use of mail is an asynchronous operation: initiated from php (sent to operating system on the server)
Look at Flash Download source of Catching Bo s30/chasebothedog a.flahttp://newmedia.purchase.edu/~Jeanine/a s30/chasebothedog a.fla
Look at html html .htmlhttp://newmedia.purchase.edu/~Jeanine/db/ html .html Note use of textarea element for multiline input
HTML Send From To Subject Body
Homework Implement your versions of BOTH the HTML and the Flash for sending Note: need to upload the send .php file and the.html file and the swf and html file published from FLASH