Lecture 9: PHP & AJAX

Web Programming

J Mwaura

Lecture Outline

  1. Introduction to PHP
  2. Arrays in PHP
  3. Communications with the server - GET and POST
  4. SQL and PHP data objects
  5. Accessing the database in PHP
  6. Introductions to AJAX
  7. Returning JSON data with AJAX
  8. Returning spatial data as geoJSON

PHP: Hypertext Preprocessor

PHP is a server side scripting language

PHP is cross-platform and supported by all major web-servers

Syntax and functionality;

  • Evaluating form data sent from the client
  • Sending and receiving cookies (to remember user data on the web)
  • Connecting to / querying databases with SQL
  • open/read/delete files on the server

Bitbucket: Hello world


Home: Hello world

Database Connection with PHP

PHP can create a secure connection to a database in order to retrieve, query, update and delete the data within

We'll make a PHP-based webpage that embeds some data from our Postgres database

Connecting to and querying a database with PHP

To create a connection to a PostgreSQL database the pg_connect() function is used which takes the host name, database name, username and password as arguments and returns a pointer to the database connection

Database is queried using the pg_query() function which returns a resource containing the results of the query, known as the results set

Connecting to and querying a database with PHP

  • pg_fetch_array() function which returns the results row by row
  • pg_fetch_array() accepts 3 arguments; the results set returned from pg_query(); the row number to fetch (if set to NULL or empty the next row is fetched) and the result type (in this case specified as an associative array
  • json_encode is used to encode the data into JSON format

Bitbucket: PHP connect


Home: PHP connect

AJAX and Javascript

PHP script can be called from within a Javascript function

The advantage in doing this is that we can draw fresh results into a webpage without reloading the whole page (for example when a button is clicked or a dropdown menu is changed)

In order to do this we need to use something called an AJAX request

Asynchronous JavaScript and XML (AJAX)

AJAX is a client side technique which allows data to be sent and retrieved from a server asynchronously, that is, without interfering with the current webpage (i.e. it runs in the background)

An AJAX request can be embedded in a JavaScript function to either send data to a database or retrieve data from a database, without having to reload the page

Asynchronous JavaScript and XML (AJAX)

  • getJSON() function is called that returns JSON encoded data from the server using a HTTP GET request
  • getJSON() function accepts two arguments: a string containing a URL to which the request is sent and a callback function which is executed if the request succeeds
  • Note how we are using the Document Object Model (document.getElementbyID) to identify the textWrap div within our HTML file

Associative Arrays/Javascript Objects

An associative array (also known as a map, dictionary or symbol table) is a collection of unique key-value pairs, where each combination of key and value appears only once in the collection

Values can be extracted from an array through reference to their key just as values in a numeric array can be extracted based on their location in the array

JSON objects is a good example of this

Web Mapping Services

A Web Mapping Service (WMS) is a service providing map data online for the use of other sites and applications

Web Mapping Services are based on the WMS standard , defined by the Open Geospatial Consortium (OGC), but other associated standards exist;

  • Web Feature Service standard for individual geographical features
  • Web Map Tile Service standard - slightly faster and modern

Web Mapping Services

Accessing Web Mapping Services

WMS defines a number of HTTP requests and what the response should be

One of the foundational requests of a WMS-compliant map server is GetCapabilities

For more

server=http://localhost:8080/geoserver/wms
service=wms
version=1.1.1
request=GetCapabilities

Server space

Setting up your own server space

  1. Setting up a machine
    • Option 1; get a PC and install OSGeo-Live, which comes with all the servers set up and configured for serving geographical data
    • Option 2: set up a Raspberry Pi as a webserver, then install the various software
  2. Getting an IP address
    • static IP address
    • dynamic IP address
  3. Getting an Domain Name
    • Buy a Domain Name from a Domain Name Registrar (list)
    • setting up various Domain Name System (DNS) records

Setting up your own server space

  1. Setting up your firewall
    • Configure your router & direct traffic to specific ports
    • install Shorewall, which makes configuring simpler
  2. Setting up Apache
    • set up our webserver for our domain name
    • Read apache documentation
    • Test server locally by typing http://localhost:80/ as a URL

OSGeo-Live - a brief introduction

OSGeo Live is an operating system designed for teaching and disseminating free open source software for geospatial applications

The package offers; map servers, desktop GIS and geographical database systems

Getting OSGeo Live

  1. Install a bootable .iso file on your computer
  2. Running OSGeo Live from a bootable USB stick
  3. Running OSGeo Live from VirtualBox

Setting up Web Mapping Services

GeoServer and MapServer are the main two Open Source technologies for serving your own map files. Equivalent of ArcGIS Server

GeoServer is available on your OSGEO Live installation

Setting up Web Mapping Services

Example - PHP + JS

Example - Events with D3 JS

End of Lecture 9

Web Programming

That's it!

Queries about this Lesson, please send them to: jmwaura@jkuat.ac.ke

*References*

  • Google Maps; Power Tools for Maximizing the API, 2014 Evangelos Petroutsos
  • D3 Tips and Tricks; Interactive Data Visualization in a Web Browser, 2013 Malcolm Maclean
  • Interactive Data Visualization for the Web, 2013 Scott Murray
  • Web Programming with HTML5, CSS, and JavaScript, 2019 John Dean
  • Leaflet Documentation Leaflet Team
  • Google Documentation for developers Google Team
Courtesy of
Web Programming