Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PHP To boldly go where JavaScript has never been…

Similar presentations


Presentation on theme: "Introduction to PHP To boldly go where JavaScript has never been…"— Presentation transcript:

1 Introduction to PHP To boldly go where JavaScript has never been…

2 JavaScript vs. PHP JavaScript is a client-side language that resides in your browser. It is available for use with no effort on your part. PHP must be installed on a server (local or remote), and you must have specific permissions to access PHP applications. JavaScript cannot access any information outside an HTML document. PHP applications can access and create files stored elsewhere in a server.

3 A typical server-side problem: A user enters information about measurements taken with a sun-viewing instrument (a sun photometer) that is used to measure total column water vapor in the atmosphere. The information provided by the user consists of the instrument’s serial number, the location and time of the measurements, and voltage outputs from the instrument. The application must use this information to find the location of the sun at the time and place of the measurement and then calculate total column water vapor based on calibration constants stored for the instrument used to collect the data.

4 The data file might look like this: SN A B C beta tau WV2-113 0.762 0.468 0.20 0.65 0.10 WV2-114 0.814 0.468 0.20 0.650.10 … WV2-157 0.911 0.468 0.20 0.65 0.10 …

5 The HTML document: Document 1.1. (getCalib.htm) Get calibration constant document.write("This document last modified on "+document.lastModified+".") Get calibration constants for water vapor instrument Enter serial number here:

6 Steps to using PHP 1.Install a server on a computer (local or remote). 2.ownload and install a PHP interpreter. 3.Configure the server to recognize PHP documents and to locate the PHP interpreter. 1. Setting up a PHP environment.

7 Steps to using PHP 2. Creating, editing, and executing PHP documents. 1.PHP documents are text files that can be created with any text editor. (A “real” code editor such as AceHTML is better.) 2. Figure out where to store PHP documents. With the WAMP server, they are stored in /wamp/www. 3. Create and edit PHP documents in an editor. 4. Execute the documents from your browser. With Windows IIS the “URL” is localhost/ {PHP file name}. 5. Make required changes in your editor and “refresh” the application in your browser.

8 Steps to using PHP 3. Passing information from an HTML Document to a PHP application. By design, this is very easy to do in PHP! The statement Automatically passes all form field values to the PHP document to a system-defined array called $_POST. In the PHP document, $SN=$_POST[“SN"]; retrieves the instrument serial number passed from Document 1.1.

9 Looking for a match… Document 1.2. (getCalib.php) <?php // Extract instrument ID from POST data… $SN=$_POST["SN"]; $len=strlen($SN); // Open WV instrument calibration constant file… $inFile = "WVdata.dat"; $in = fopen($inFile, "r") or exit("Can't open file"); // Read one header line… $line=fgets($in); // Search rest of file for SN match… $found=0; while ((!feof($in)) && ($found == 0)) { $values=fscanf($in,"%s %f %f %f %f %f"); list($SN_dat,$A,$B,$C,$beta,$tau)=$values; if (strncasecmp($SN_dat,$SN,$len)==0) $found=1; } fclose($in);

10 Several ways to read data from file… $values=fscanf($in,"%s %f %f %f %f %f"); list($SN_dat,$A,$B,$C,$beta,$tau)=$values; $line=fgets($in); list($SN_dat,$A,$B,$C,$beta,$tau)=sscanf($line,"%s %f %f %f %f %f"); Read values from the file into an array, according to a specified format. Then assign the elements to named variables: Read an entire line as a string and then read values from the string according to a specified format: fscanf($in,"%s %f %f %f %f %f",$SN_dat,$A,$B,$C,$beta,$tau); Read an entire line and assign named variables directly, according to a specified format:

11 Processing the data… if ($found == 0) echo "Couldn't find this instrument."; else { // Build table of outputs… echo " Quantity Value "." "; echo " Instrument ID $SN "; echo " Calibration Constants "; echo " A $A "; echo " B $B "; echo " C $C "; echo " τ $tau "; echo " β $beta "; echo " "; } ?>

12 Saving your results on a server… $inFile = "WVdata.dat"; $outFile="WVreport.csv"; $in = fopen($inFile, "r") or exit("Can't open file."); $out=fopen( "c:/Documents and Settings/All Users/Documents/PHPout/".$outFile,"a"); fprintf($out, "Data have been reported for: %s,%f,%f,%f,%f,%f\n", $SN,$A,$B,$C,$tau,$beta); fclose($out);

13 Solving a quadratic equation For the quadratic equation ax 2 + bx + c = 0, find the real roots: r1 = [-b + (b2 – 4ac)1/2 ]/2a r2 = [-b - (b 2 – 4ac)1/2 ]/2a The “a” coefficient must not be 0. If the discriminant, b 2 – 4ac = 0, there is only one root. If the discriminant is less than 0, there are no real roots.

14 Document 1.6a (quadrat.htm) Solving the Quadratic Equation Enter coefficients for ax 2 + bx + c = 0: a = (must not be 0) b = c =

15 Document 1.6b (quadrat.php)

16 Preventing multiple form submissions Document 1.7a (WeatherReport.htm) Weather Report var alreadySubmitted = false; function submitForm ( ) { if (alreadySubmitted) { alert("Data already submitted. Click on 'Reset Button' and start over." ); return false; } else { alreadySubmitted = true; return true; } }

17 Multiple submissions, part 2 Report weather observations <form method="post" action="WeatherReport.php" onSubmit="return submitForm(this.form);" > Date (mm/dd/yyyy) : Time (UT hh:mm:ss): Air temperature (deg C): Barometric pressure (millibar): Cloud cover (octas 0-8): Precipitation today (total mm): This code allows only one “submit,” after which you have to click the Reset button. Note that this is a JavaScript solution, but the problem often occurs when you are submitting data to a PHP application that appends to an existing file.


Download ppt "Introduction to PHP To boldly go where JavaScript has never been…"

Similar presentations


Ads by Google