PHP Fundamentals and Form Processing

PHP Fundamentals and Form Processing

An Introduction to PHP

"PHP is an open source general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages. It is one of the first developed server-side scripting languages to be embedded into an HTML source document rather than calling an external file to process data. The code is interpreted by a Web server with a PHP processor module which generates the resulting Web page. It also has evolved to include a command-line interface capability and can be used in standalone graphical applications. PHP can be deployed on most Web servers and also as a standalone shell on almost every operating system and platform free of charge. A competitor to Microsoft's Active Server Pages (ASP) server-side script engine and similar languages, PHP is installed on more than 20 million Web sites and 1 million Web servers. Software that uses PHP includes MediaWiki, Joomla, Wordpress, Concrete5, MyBB, and Drupal." (from wikipedia)

General Notes on PHP

The following provides a summary of what PHP is:

  • PHP stands for PHP: Hypertext Preprocessor
  • PHP is a server-side scripting language, like ASP, JSP, etc.
  • PHP scripts run on the server.
  • PHP supports many databases, e.g., MySQL, Oracle.
  • PHP is an open source software.

How to Start Your First PHP Program?

The CS Hercules Server supports PHP, so you don't need to do anything on it, for example, installation or configuration. The only thing you need to do is to create a .php file in the public_html directory. Note: you still need to set the correct permission. To sumarize you can follow the steps below:

  1. Create a .php file.
  2. Upload the .php file into the public_html folder.
  3. Set the permission for the php file:
    ssh hercules
    cd public_html/
    chmod 644 *.php
  4. Open your browser, input and visit the following address:
    http://www2.cs.uregina.ca/~your_hercules_username/filename.php

Where to Write Your PHP Scripts?

A PHP script always starts with a pair, <?php and ?>, for example,

<?php
 echo "<h1>Hello CS215 Students!<h1>";
 echo "<p>Hi this is a sentence.</p>";
?>

A PHP Server (the Hercules) parses the PHP scripts and returns the plain HTML code back to the client browser. That is, the echofunction sends a level-one header (H1) and a paraghraph (P) to your browser. You can try this example on Hercules.

PHP files can contain text, HTML tags and scripts, so your skills of HTML, CSS and JavaScript are usful in writing a .PHP file (Anyway, all the PHP scripts will be parsed into the plain HTML code) . The following PHP script is written mixed with HTML.

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>A Sample Php Program</title>
</head>

<body>

<?php
	echo "<h1>Hello CS215 Students!<h1>";
?>

</body>
</html>

In Class Exercise Exercise 1

  1. Create and run a simple .php page on Hercules.
  2. You can visit your php page by using the following url:
    http://www2.cs.uregina.ca/~ur_herucles_username/filename.php

Syntax of PHP Scripts

The following introduces basic syntax of PHP scripts, including variables, operators, control structures, arrays, functions:

PHP Variables

Variables in PHP starts with a $ sign, followed by the name of the variable. The following link provides the detail.

PHP variables: http://www.w3schools.com/php/php_variables.asp

Operators

PHP script language provides arithmetic operators, assignment operators,comparison operators, increment/decrement operators, logical operators, string operators and array operators. The following link provides the detail.

PHP Operators: http://www.w3schools.com/php/php_operators.asp

Control Structures

PHP control structures includes conditional statetment and loop statement. The following links provide the detials.

Conditional statement: http://www.w3schools.com/php/php_if_else.asp

Loop statement: http://www.w3schools.com/php/php_looping.asp

Functions and Arrays

Functions: http://www.w3schools.com/php/php_functions.asp

Array: http://www.w3schools.com/php/php_arrays.asp

An Example of PHP

Example:

This is the php example of page that will display four sentences each with a different color. The text alternates between the number of the sentence and the color of the sentence .

<!DOCTYPE html>
 
<html>
<head>
 </head>
<body>
<h1> Hello CS215 Students! </h1>

<?php
echo "<p>Hi this is a sentence written using echo ... .</p>";
$colors = array("red","green","blue","yellow");
for ($x=0; $x<4; $x++) {
	
     echo '<p style="color:'.$colors[$x].'" > ';  
	 
	 
	 if ($x%2==0) {
	 	echo "The sentence number is: $x   </p>";
	 } else {
	 echo "The  sentence  color is: $colors[$x]  </p>";
	 }
	 
}
?> 

</body>
</html>

PHP Form Processing

In this section, you will use PHP to obtain user inputs in an HTML form, validate the HTML form and learn to upload files to a folder on the server.

PHP Form Handling

The server-side script language, PHP, can be used to process user inputs from client-sides. Two global associative arraies are used to obtain inputs, namely, $_GET and $_POST. The former corresponds to the get method of an HTTP request, while the latter corresponds to the post method of an HTTP request. The following link provides the detail of PHP form handling.

PHP form handling: http://www.w3schools.com/php/php_forms.asp

Form Validation

You need server-side data validation, although you have already validated user inputs by JavaScripts at the client-side.

PHP form validation: http://www.w3schools.com/php/php_form_validation.asp

An Example of PHP Form Handling: File Uploading

File uploading: http://www.w3schools.com/php/php_file_upload.asp

Object Oriented PHP

Object Oriented Programming in PHP, refers to the method of programming PHP that invokes the use of classes to organize the data and structure of an application. With PHP, OOP was introduced in PHP 4, but really built ready for PHP 5.

W3Schools PHP 5 Tutorial

Object Oriented PHP

Troubleshooting Your PHP

Are You Getting a Blank Page? If so, you can try a couple of things:

run your code through the command line php:
php sqlphp.php (It should be done from hercules machine)

This will indicate if there are syntax errors such as missing semi-colons, curly brackets, or quotation marks. You might also get the same error messages when the .php code is executed on a server.

http://www2.cs.uregina.ca/~your_hercules_username/filename.php

In Class Exercise Lab Assignment

Note: There are two exercises in this lab.

Exercise One

Create an html form up.html which is capable of uploading four photos to a directory called upload(permissions 777) in your public_html directory. You will use php to upload the files and display the files in a table format. Your output should look similar to the following images.

pic1

pic2

Breakdown

First Page --- (up.html)

  • Four inputs for photo filenames
  • Four inputs for photo captions
  • Four working radio buttons for adjusting the color of the resulting page
  • A submit and a reset button

Second Page which is generated by PHP --- (up.php)

  • Four pictures appearing with captions underneath the pictures
  • Background color changes according to the radio button selected. style background color example
  • The pictures are in a table

caution log *When the marking is done. change the permisions of your upload folder back to 755.

Having the permission set to 777 will open it up for the whole world to see and edit.

Exercise Two

  1. Complete the previous medical form you can also name your file php_form.html
  2. Your seperate php file ---( med_proc.php) should display user inputs.
  3. Error checking is required. If one field is missing, warning messages should be displayed.
  4. Here is an example on how to get values of multiple checked checkbox. www.formget.com/php-checkbox
  5. Your output should look similar to the following image.
  6. sample.output

Again, link your up.html and php_form.html on your index.html

Zero will be given if the above files are not linked on your index.html

Zip the following files and name it Yourusername_lab9.zip. Upload in URcourses by 11:55 PM.

  1. up.html
  2. up.php
  3. php_form.html
  4. med_proc.php