Chapter 5: Developing web applications for bioinformatics

As outlined in the introduction of this book, developing web applications is no different from developing any other software: the developer needs to be able to get some kind of input from the user, typically in the form of data and/or options, to elaborate this input and finally to provide an output to the user. In web applications the input is gathered through a web form, that we have covered in the last section of chapter 3. Most of the times the “POST” method will be used to submit user data to a processing script. In PHP, the script can then access the data through the built-in $_POST array, perform the elaborations required and then provide an output in the form of an HTML web page.

In the following sections we will make examples of data elaboration, largely based on the PHP scripts and code samples already developed in the previous chapter.

For now, let’s make a very simple example to see how we can grab the data submitted through the web form by using the $_POST array in the script, and then echo it back to the user, with just a minimal elaboration of a sequence, while we are at it. The script will work best with short sequences and will not handle FASTA format.

The web form has 3 fields:

– The sequence field (name=”sequence”)
– The color field (name=”color”)
– The case field (name=”case_format”)

The web form of the sample script in this page
The web form of the sample script in this page

Its is quite similar to the one proposed in section 3-11.

As output, we inform the user on the choices he made by echoing back his sequence (as it was) and the color and case selections that were made. We also output a version of the sequence in the selected case (uppercase or lowercase) and colored according to the color selection.

The script output with a simple test sequence
The script output with a simple test sequence

The code, HTML, CSS and PHP, is distributed in several files organised as follows. The directories names are bolded.

ex5-1
    index.php
    script.php
    html
        header.html
        footer.html
    css
        style.css

The code of the script is generously commented so that you should be able to understand each single line. Please do read the comments as they contain useful insights into the code logic. This applies in particular to the code of the script.php file.

header.html

footer.html

index.php

style.css

script.php

In this example, the web form has just three fields. Each field has it’s own value for the “name” attribute of the field.

The name for the sequence field is “sequence”. Therefore, in the script, we can access the sequence posted by the user at

$_POST[“sequence”]

That is the key “sequence” within the $_POST associative array. This key was automatically created for us by PHP at the time of form submit. Indeed a key is created in the $_POST array for each of the names used for the fields in the web form.

In the script we store this value in a variable called, itself, $sequence. We warmly recommend that you always use the names given in the web form as names for the corresponding variables in the script, it makes everything simpler and it also makes a lot of sense.

selecting variables names in a form processing script
In selecting variables names in a form processing script it is a great idea and good programming practice to use the same names as those used in the web form as values for the “name” attributes of the form fields.

We also recommend that you start a form processing script by importing the values from the $_POST array as show in the example above. Rather than importing them somewhere in the script, when you need them, maybe in a scattered way, it is much cleaner to import all of them right at the beginning, or toward the beginning of the script so that they become immediately available for whatever you need to do in the script.

Ideally, a web form processing script could be subdivided in three parts:

– Importing the data from the web form (and maybe other files, scripts or libraries)

– Elaborating data

– Providing an output

The following figure shows the script in this page with these three parts highlighted.

The ideal structure for a form processing script
The ideal structure for a form processing script

You may run this script live here.

Chapter Sections

[subpages]

WORK IN PROGRESS ON CHAPTER 5!

Leave a Reply

Your email address will not be published. Required fields are marked *