Download presentation
Presentation is loading. Please wait.
Published byGordon Patrick Modified over 8 years ago
1
Introduction to Dynamic Web Content Chapter 1 Dr. Charles Severance To be used in assocition with the book: PHP, MySql, and JavaScript by Robin Nixon
2
Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/. Copyright 2011-2013, Charles Severance
3
Early History of Important* Programming Languages Machine Language (1940's) Assembly Language (1940's) FORTRAN - Mathematics expressions (1955) C - High-productivity Assembly Language (1969) * As defined by Dr. Chuck
4
Science Calculations System http://en.wikipedia.org/wiki/History_of_programming_languages System Scripting/ Interpreted Scripting/ Interpreted C uses curly braces { } for code blocks.
5
Show Me The Money... C, C++, C#, ObjectiveC Java PHP... http://langpop.com/ http://www.flickr.com/photos/tracy_olson/61056391/
6
The History of PHP Rasmus Lerdorf Not a trained computer scientist Consultant building dynamic web sites - got tired of doing the same thing over and over in C Reusable bits + HTML Templates http://www.vimeo.com/6215179
7
Request / Response Cycle You enter http://server.com into your browser’s address bar. Your browser looks up the IP address for server.com. Your browser issues a request for the home page at server.com. The request crosses the Internet and arrives at the server.com web server. The web server, having received the request looks for the web page on its hard disk. The web page is retrieved by the server and returned to the browser. Your browser displays the web page.
10
Internet HTML CSS JavaScript AJAX HTTP Request Response GET POST PHP Templates MySql MVC Cookies
11
Our Technologies
12
PHP <?php echo "Hello World. Today is ".date("l").". "; ?>How are you? Hello World. Today is Wednesday. How are you? PHP is the most widely used scripting language for web programming. PHP extends HTML pages by adding server-executed code segments to HTML pages. The output of the execution of the PHP code is merged into the HTML page.
13
MySQL INSERT INTO users VALUES('Smith', 'John', 'jsmith@mysite.com'); SELECT surname,firstname FROM users WHERE email='jsmith@mysite.com'; MySQL is one of the most popular free and open source database engines in the market place. MySQL powers Facebook, Yahoo!, WordPress, Drupal, Joomla, and millions of other dynamic web sites.
14
JavaScript document.write("Hello World. Today is " + Date() ); JavaScript is a C-like programming language that can be included in an HTML web page. JavaScript allows the builder of a web page to embed dynamic elements within a web page. JavaScript programs run in the browser (i.e. the Client)
15
JavaScript: Brendan Eich Invented JavaScript in May 1995 in ten days
16
Apache Web Server Originally Developed at National Center for Supercomputing Applications in 1994 Open Source First project / product of the Apache Foundation Brian Behlendorf founded Apache http://www.vimeo.com/3800796
17
Summary We laid out the general structure for the course and introduced the technologies that will be used for the course.
18
Setting up a Development Server Chapter 2 Dr. Charles Severance To be used in associaton with the book: PHP, MySql, and JavaScript by Robin Nixon
19
Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/. Copyright 20111-2012, Charles Severance
20
What We Need Apache Web Server (Includes PHP) MySql Database Server MySql Administation Software (PhpMyAdmin) We could install all this by hand...
21
All-In-One Pre-Packaged WAMP - Windows, Apache, MySQL, and PHP MAMP - Mac, Apache, MySQL, and PHP LAMP - Linux, Apache, MySQL, and PHP XAMPP Prefer: MAMP on Macintosh XAMPP on Windows
22
Outline of Tasks Install text editor Download and install XAMPP Create a Simple PHP file
23
Programmer Editor Do not use Word, TextEdit, or NotePad Programmer Editor Windows: NotePad++ Macintosh: TextWrangler Feel free to use an Integrated Development Environment such Eclipse, etc. if you have experience.
24
Video
25
Assignment Deliverables
27
Possible Issues Macintosh installs will require superuser password Windows installs Will require superuser password May ask for permission to use ports May conflict with your firewall Sometimes one product (XAMPP) works and an another does not Sometimes a port gets "stuck" and a reboot is required (mostly Windows) Start early and get help
28
Introduction to PHP Chapter 3 Dr. Charles Severance www.php-intro.com To be used in association with the book: PHP, MySql, and JavaScript by Robin Nixon
29
Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/. Copyright 2011- Charles Severance
30
About the PHP Language Syntax is inspired by C Curly braces, semicolons, no signficant whitespace Syntax inspired by perl Dollar signs to start variable names, associative arrays Extends HTML to add segments of PHP within an HTML file.
31
Philosphy of PHP You are a responsible and intelligent programmer You know what you want to do Some flexibility in syntax is OK - style choices are OK Lets make this as convienent as possible Sometimes errors fail silently
32
Hello from Dr. Chuck's HTML Page <?php echo "Hi there.\n"; $answer = 6 * 7; echo "The answer is $answer, what "; echo "was the question again?\n"; ?> Yes another paragraph.
33
Hello from Dr. Chuck's HTML Page <?php echo "Hi there.\n"; $answer = 6 * 7; echo "The answer is $answer, what "; echo "was the question again?\n"; ?> Yes another paragraph.
34
PHP From the Command Line You can run PHP from the command line - the output simply comes out on the terminal It does not have to be part of a request-response cycle <?php echo("Hello World!"); echo("\n");?>
35
Key Words http://php.net/manual/en/reserved.php abstract and array() as break case catch class clone const continue declare default do else elseif end declare endfor endforeach endif endswitch endwhile extends final for foreach function global goto if implements interface instanceof namespace new or private protected public static switch $this throw try use var while xor
36
Variable Names Start with a dollar sign ($) followed by a letter or underscore, followed by any number of letters, numbers, or underscores Case matters http://php.net/manual/en/language.variables.basics.php $abc = 12; $total = 0; $largest_so_far = 0; abc = 12; $2php = 0; $bad-punc = 0;
37
Variable Name Weirdness Things that look like variables but are missing a dollar sign can be confusing $x = 2;$y = x + 5;print $y;$x = 2;y = $x + 5;print $x; 5Parse error
38
Expressions Completely normal like other languages ( + - / * ) More agressive implicit type conversion 42
39
Output echo is a language construct - can be treated like a function with one parameter. Without parenthesis, it accepts multiple parameters. print is a function - only one parameter but parenthesis are optional so it can look like a language construct <?php $x = "15" + 27; echo $x; echo ("\n"); echo $x, "\n"; print $x; print "\n"; print($x); print("\n"); ?>
40
Conditional - if Logical operators ( == != = and or ! ) Curly braces <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> Hello World!
41
Whitespace does not matter <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; }?>
42
What Style do You Prefer? <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> Aesthetics
43
Associative Arrays Like Python Dictonaries+Lists - but more powerful Can be key => value or simply indexed by numbers Ignore two-dimensional arrays for now...
44
Integer Indices <?php $stuff = array("Hi", "There"); echo $stuff[1], "\n"; ?> There
45
Integer Indices <?php $stuff = Array(); $stuff[] = "Hello"; $stuff[] = "World"; echo $stuff[1], "\n"; ?> Word
46
Integer Indices <?php $stuff = Array(); $stuff[2] = "Hello"; $stuff[9] = "World"; echo $stuff[9], "\n"; ?> World
47
Key / Value <?php $stuff = array("name" => "Chuck", "course" => "SI664"); echo $stuff["course"], "\n";?> SI664
48
Dumping an Array The function print_r() dumps out PHP data - it is used mostly for debugging <?php $stuff = array("name" => "Chuck", "course" => "SI664"); print_r($stuff); ?> Array([name] => Chuck [course] => SI664)
49
Dumping an Array The function print_r() dumps out PHP data - it is used mostly for debugging <?php $stuff = Array(); $stuff[2] = "Hello"; $stuff[9] = "World"; print_r($stuff); ?> Array ( [2] => Hello [9] => World )
50
var_dump.vs. print_r <?php $stuff = array("name" => "Chuck", "course" => "SI664"); var_dump($stuff); ?> array(2) { ["name"]=> string(5) "Chuck" ["course"]=> string(5) "SI664"} http://stackoverflow.com/questions/3406171/php-var-dump-vs-print-r
51
var_dump() is more verbose <?php $thing = FALSE; echo("One\n"); print_r($thing); echo("Two\n"); var_dump($thing); ?> One Two bool(false) http://stackoverflow.com/questions/3406171/php-var-dump-vs-print-r
52
Looping Through an Array <?php $stuff = array("name" => "Chuck", "course" => "SI664"); foreach($stuff as $k => $v ) { echo "Key=",$k," Val=",$v,"\n"; } ?> Key=name Val=Chuck Key=course Val=SI664
53
Variable Name Weirdness Things that look like variables but are missing a dollar sign as an array index are unpredictable.... $x = 5; $y = Array("x" => "Hello"); print $y[x]; Hello
54
Strings String literals can use single quotes or double quotes The backslash (\) is used as an "escape" character Strings can span multiple lines - the newline is part of the string In double-quoted strings variable values are expanded http://php.net/manual/en/language.types.string.php
55
<?php echo 'this is a simple string'; echo 'You can also have embedded newlines in strings this way as it isoka y to do'; // Outputs: Arnold once said: "I'll be back“ echo 'Arnold once said: "I\'ll be back"'; // Outputs: This will not expand: \n a newline echo 'This will not expand: \n a newline'; // Outputs: Variables do not $expand $either echo 'Variables do not $expand $either'; ?> Single Quote
56
<?php echo "this is a simple string\n"; echo "You can also have embedded newlines in strings this way as it is okay to do"; // Outputs: This will expand: // a newline echo "This will expand: \na newline"; // Outputs: Variables do 12 $expand = 12; echo "Variables do $expand\n";?> Double Quote
57
http://php.net/manual/en/language.basic-syntax.comments.php <?php echo 'This is a test'; // This is a c++ style comment /* This is a multi line comment yet another line of comment */ echo 'This is yet another test'; echo 'One Final Test'; # This is a shell-style comment?>
58
Summary This is a sprint through the language features of PHP
59
Forms and PHP Chapter 11 Dr. Charles Severance www.php-intro.com To be used in association with the book: PHP, MySql, and JavaScript by Robin Nixon
60
Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/. Copyright 2011- Charles Severance
61
Forms Submit Data Guessing game... Input Guess form1.php
62
$_GET and $_POST PHP loads the values for the URL parameters into an array called $_GET and the POST parameters into an array called $_POST There is another array called $_REQUEST which merges GET and POST data
63
Guessing game... Input Guess $_GET: <?php print_r($_GET); ?> form2.php
64
Guessing game... Input Guess $_POST: <?php print_r($_POST); ?> $_GET: <?php print_r($_GET); ?> form3.php
65
<?php $oldguess = isset($_POST['guess']) ? $_POST['guess'] : ''; ?> Guessing game... Input Guess /> form4.php
66
Hygene Alert! What happens when we use an HTML character in a form field value?? form4.php
67
Input Guess DIE DIE “ /> form4.php
68
To The Rescue: htmlentities() Input Guess /> form5.php
69
Input Guess <input type="text" name="guess" id="guess"<?php echo 'value="'. htmlentities($oldguess). '"';?> />
70
Processing POST Data There are many patterns for handling POST data No "rules" just "suggestions" <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } }?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); }?> Input Guess <input type="text" name="guess" id="guess" size="40“ <?php echo 'value="'. htmlentities($guess). '"';?> /> Completely process incoming POST data (if any) - produce no output. Produce the page output. guess.php
71
<?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"';?> /> < /form> guess.php
72
<?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"';?> /> < /form> <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Nifty trick $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ){ $message = "Too low"; } else { $message = "Too high..."; } }?>...
73
<?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"';?> /> < /form>... ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); }?> Input Guess />
74
Other Input Types Text Password Radio Button Check Box Select / Drop-Down TextArea more.php
75
Many field types... Account: Password: Nick Name: more.php $_POST:Array( [account] => Beth [pw] => 12345 [nick] => BK [when] => pm...)
76
Preferred Time: AM PM more.php $_POST: Array(... [nick] => BK [when] => pm [class] => si502...)
77
Classes taken: SI502 - Networked Tech SI539 - App Engine SI543 - Java more.php $_POST: Array(... [when] => pm [class] => si502 [soda] => 0...)
78
Which soda: -- Please Select -- Coke Pepsi Mountain Dew Orange Juice Lemonade more.php $_POST: Array(... [class] => si502 [soda] => 0 [snack] => peanuts...)
79
Which snack: -- Please Select -- Chips Peanuts Cookie more.php $_POST: Array(... [class] => si502 [soda] => 0 [snack] => peanuts...)
80
Tell us about yourself: I love building web sites in PHP and MySQL. more.php $_POST:Array(... [about] => I love building web sites in PHP and MySQL. [dopost] => Submit...)
81
more.php $_POST:Array(... [dopost] => Submit...)
82
Summary Forms, $_GET and $_POST Sanitizing HTML
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.