1. Computing

Using PHP and Javascript instead of CGI

From , former About.com Guide

1. Introduction

So you have moved to a new job in data processing as part of web application development. At your old job, you used CGI to process forms, but the new server does not allow CGI. The hotshot AJAX guru who left only has PHP and Javascript. You don't have time to muck about with Apache. What's a Python programmer supposed to do? Flourish and excel, and this tutorial will show you how.

2. About PHP and Javascript

PHP and Javascript are, in a sense, just two sides of the same coin. Javascript is for programming you want executed on the user's desktop, also known as "client-side scripting". Javascript is able to watch the user's actions and initiate behavior based upon it. This is the secret to the special effects seen in many AJAX applications, like Ask.com Maps or Shop.com.

3. Javascript: The submit() Function

Everything that happens in the world of Javascript happens on the user's computer. The user can see all of the code that is written, if necessary. For this reason, one should not use Javascript for handling sensitive data like passwords and personal information. The same applies to your own data. If you would rather not disclose how to access your database, you may prefer to keep the actual system calls away from Javascript. For this, you need scripting on the server. Enter PHP.

4. PHP: "PHP: Hypertext Preprocessor"

PHP is a recursive acronym (the acronym of its name is part of the acronym for which its name stands) and means "PHP: Hypertext Preprocessor." All of that is a lot of lingo to say that PHP is a scripting language that generates HTML. Within a HTML document, it can be used to generate new HTML on the fly. For our purposes, it will serve as a CGI alternative to get data to our Python script.

Why could we not simply pass the form data directly to our program? Without CGI, there is no dialogue with the web server. There is no way of knowing whether the program actually worked without feedback. In the ensuing program, PHP serves as a relay for communication between the web page and the server.

5. Passing Form Data With Javascript

To pass form data using Javascript, use the function 'submit()'. The syntax of submit is 'submit(<form_data>)'. So, in HTML, one writes as follows:

 <form action="search.php" method="post"> 
 <h5> Search <br> 
 <input type='text' name='word' size='20' onChange="submit(word)"><br> 
 </h5> 
 </form> 
 

This will allow the user to submit the form by simply pressing 'Enter' instead of clicking on 'Submit'. As you can tell, the form data will be passed to a PHP script called 'search.php' which resides in the same directory. Putting a script in the same directory may sound like a security issue at first, but PHP is designed for discretion and will return an empty HTML document instead of code when accessed directly.

To be fair, one could use a simple HTML form here, but the Javascript allows one to offer the visitor to your website a better experience.

6. PHP as a CGI stand-in

Now we need to create the PHP script that will channel communication between our Python program and the web page. The basic code is below.

We will discuss this script on the next page. If you would like to keep the code to hand for reference, copy it into your favorite editor or open this page in a new window.

 <?php 
 $results = shell_exec ("./search \"" . $_POST["word"] . "\" 3") ; 
 
 print " 
 <html> 
 <head> 
 <title> About.com's Python Program Results.</title> 
 <link rel=\"stylesheet\" type=\"text/css\" href=\"./myway.css\" /> 
 </head> 
 <body> 
 
 <div class=\"search\"> 
 <form action=\"search.php\" method=\"post\"> 
 <h5>Search <br> 
 <input type=\"text\" name=\"word\" size=\"20\" onChange=\"submit(word)\"><br> 
 <!-- <input type=\"submit\" value=\"Submit\" /> </h5> --> 
 </form> 
 </div> 
 
 <h1 class=\"welcome\"> Welcome to Python with PHP and Javascript </h1> 
 
 <h6 class=\"maintext\"> "; 
 
 print $results; 
 print "</h6> </body> </html>"; 
 ?> 

7. Looking at the PHP script: Part 1

All PHP scripts begin with '<?php' and end with '?>'. When set within a page of HTML , these indicators tell the computer where to differentiate between HTML and PHP.

The next line is a PHP statement that does three things. It takes the form data from the web page and forms a terminal command, a system call, from it, passing the form data to the Python program as a command line argument. The trailing '3' is simply the second argument for the Python program, sys.argv[2]. Where CGI requires a bit of hacking to handle multiple options, PHP allows you to hardwire options into the script, thus allowing you to call the same script in different ways from various pages.

This system call is then executed using the PHP command 'shell_exec()', which runs the Python program and returns the output. This output is then assigned to the variable '$results'. It is important to note that variables in PHP start with a dollar sign ('$') and that all PHP statements must end with a semi-colon (';').

8. Looking at the PHP script: Part 2

The main output of the script, contained within the first 'print' statement, is a basic re-creation of the earlier webpage. While validation is important, we do not include that markup for reasons of space. If you are unfamiliar with CSS, the 'class' arguments in the HTML refer to subsections of the CSS stylesheet. In this example, I am relying on CSSP to position the output as I would like.

The next print statement prints the contents of $results to the new web page. This PHP script will output anything that comes from the Python program. The flexibility of Python is preserved. Instead of printing search results, as this setup obviously does, this statement could simply say "Thank you for your order. Your Pentalawn 2000 will arrive within the next three weeks."

The third print statement simply closes the HTML markup before the script finishes.

9. Concluding Comments

Using this process of Javascript form submission and PHP data handling, the Python programmer has a sleak system that is easier to work with than CGI and is more flexibile as well. Keeping the output in PHP, the Python program code stays cleaner and is thus easier to maintain (as if that were possible!).

©2013 About.com. All rights reserved.