Website Development & Management PHP Fundamentals CIT Fall Instructor: John Seydel, Ph.D.
Student Objectives Upon completion of this class meeting, you should: Be able to discuss the concepts involved in writing PHP scripts that Collect data sent by an HTML form Assign values to local variables based upon form data Perform simple calculations Display data and calculation results back to a web page Have resolved questions regarding the WAMP installation Summarize the markup needed to create web forms with commonly used controls Textbox Checkbox Radio button Drop-down list Submit button
PHP Script Basics Variable naming Requirements Conventions Assignment statements Literals Numeric Text Variables Getting form data from global arrays Displaying values Basic calculations Control structures (basic selection)
Review: PHP Platform Installation Installing MySQLMySQL Installing/configuring ApacheApache Installing/configuring PHPPHP Note: these instructions are intended to augment, not replace the textbook’s instructions
Some More Details on HTML Forms Main element is Attributes: id (for object references in scripts) action (where the form processor is) method (usually post but sometimes get) Should be only one per page (for our purposes) All controls must be within... Form controls input (many types) input select select button (we won’t be using this, probably) textarea Note the importance of the value attribute for these controlsvalue Forms represent one of the major objects in the DOMDOM Recall: processors can be either server-side or client-side Any questions... ?questions
Summary of Today’s Objectives Resolve questions regarding the WAMP installation Develop ability to write a simple PHP-based form processor that Collects data sent by an HTML form Assigns values to local variables based upon form data Performs simple calculations Displays data and calculation results back to a web page
If Time Permits... Demonstration: web pages are HTML files and are thus independent of the editors used and the means used to upload/download them Capture CoB web page students.htm This is a DreamWeaver page Edit in NotePad Use FrontPage to upload back to CoB site Questions Troubles encountered when installing WAMP? Why don’t we meet in the lab? Other... ? Using phpinfo() to view form data
Appendix
The Installation Process: MySQL Generally follows procedure given in Meloni textbook Download mysql win.zip from course Handouts page (or from MySQL.com) into Downloads directory Uncompress into a default temporary directory and then open that directory Double-click on SETUP.EXE and accept all defaults as the installation wizard runs Test the installation Run c:\mysql\bin\winmysqladmin.exe Provide a username and password you’ll remember (generally, these won’t be used again, ever) Note the stoplight now on taskbar at bottom right MySQL’s database server now starts upon bootup For now, go no further (i.e., not beyond p. 8)
The Installation Process: Apache Again, ensure that IIS is stopped Also follows procedure given in Meloni textbook but with some variations; assumes you’ll be installing PHP right away Download apache_ win32-x86-no_ssl.msi from course Handouts page (or from Apache.org) into Downloads directory Double-click on apache_ win32-x86-no_ssl.msi Accept defaults as the installation wizard runs Don’t worry for now about Server Information panel Verify the installation Note the feather icon near the right end of the taskbar Point browser to (output displayed on p. 29 should appear) Modify the configuration...
Modifications to Apache Edit the configuration file: c:\Program Files\Apache Group\Apache2\conf\httpd.conf Specify server settings: Find ServerAdmin statement and set the argument to your address Find ServerName statement and set the argument to your IP address or to Two more changes: Add to the end of the LoadModule section LoadModule php5_module c:/php/php5apache2.dll Add to the end of the AddType section AddType application/x-httpd-php.phtml.php Do not restart Apache until after installing PHP
The Installation Process: PHP Varies somewhat from procedure given in Meloni textbook Download php Win32.zip from course Handouts page (or from PHP.net) into Downloads directory Uncompress into a permanent directory (c:\php) and then open that directory Configure PHP (see next slide) Restart Apache Click on taskbar icon (feather) Then Apache2 | Restart
Configuring PHP Make changes to files Copy c:\php\php.ini-dist to c:\WINDOWS Rename c:\WINDOWS\php.ini-dist to c:\WINDOWS\php.ini Edit c:\WINDOWS\php.ini Open in NotePad Find and uncomment (remove “;”) extension=php_mysql.dll Find and set error reporting configuration error_reporting = E_ALL & ~E_NOTICE display_errors = On Setup for (example) SMTP = smail.astate.edu sendmail_from = Save and close the file Copy c:\php\php5ts.dll to c:\WINDOWS\system Copy c:\php\ext\php_mysql.dll to c:\WINDOWS\system Copy c:\mysql\bin\libmysql.dll to c:\WINDOWS\system Restart Apache
Browser/Server Interaction
The HTML Document Object Model Option Browser objects: objects that provide access to the browser
Input Elements Empty element with many controls (per the type attribute) One line text box (text)text Check boxes (checkbox)checkbox Option buttons (radio)radio Submit/reset button (submit or reset) Other common attributes name (always specify) size (also maxlength, but not same) value
Text Boxes Format: Simplest type of input Disadvantage: can’t control what’s typed except through Using maxlength JavaScript validation is better Fonts: Can’t be controlled, except with style sheets Size differs across browsers
Check Boxes Format: Each box has a different name and set of attributes Notes about attributes Value submitted for value attribute If checked then whatever value is specified Else “” The checked attribute is binary (0 or 1) Don’t confuse with option buttons
Option Buttons Format: Must define an option group That is, multiple controls all with same name Each control within group has different value Only one of any group can be checked at a time When form is submitted, only the value of the checked control will be sent Like multiple choice question: Option button ~ “mark the best answer” Check box ~ “mark all that apply”
Select (“Combo” or “List”) Box Involves two elements (main element; container) (contained in select element) Main select attributes name size Event handlers (e.g., onChange) Primary option attributes value selected
Buttons, Buttons, Everywhere Three kinds of buttons (not including reset) element (empty element) Server-side processing: Client-side processing: element: contains inline & block elements Client-side processing only Allows control over display (images, font, etc.) Format:... content... A note on client-side processing Button requires the onClick attribute Invokes the JavaScript function specified
The value Attribute Can be preassigned to a control Shows up in text controls and on buttons With other controls, values are hidden and are assigned to variables associated with controls When form is processed, value is whatever is entered into control If text box or text area, then text string Otherwise it’s a preassigned value Check boxes Buttons: submit, reset, option, button Select (combo) boxes
Names to Use for Controls Use conventions from programming languages: Java, C++, Visual Basic Typically First 3 characters lowercase and type of control Uppercase only when words joined (intercap) Control TypePrefixExample TextboxtxttxtFirstName Text AreatxttxtFeedback CheckboxchkchkRetirement Option ButtonoptoptClass Dropdown ListlstlstWeekDay Submit ButtoncmdcmdCalculatePay