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.
<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.
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.
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. 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 (';').
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.