Friday, September 9, 2011

A closer look at PHP database update options

A frustration I've had with most tutorials on using JavaScript with PHP to update a database is that the "add data" function nearly always calls a new page. In my application, I want data added to a database quietly in the background, and I certainly don't want the user dragged away from the Applet page every time a line is added to the database.

After much searching, and searches excluding terms such as "forum", (forum questions seem to dominate this topic in Google), I found an excellent page hosted by Virginia Tech. This is a really good single page tutorial, which takes the reader through all the basics of PHP, from echo "Hello World!"; to a simple web application, which includes all the essential functions of adding data, displaying data, editing data and deleting records. Better still, for each task, it includes the PHP source code, produced HTML, and rendered HTML. There are no zip files to download, and certainly at first glance, no holes or gaps in the code.

Most importantly, for me, the tutorial makes explicitly clear the code required to stay on the same page while information is being posted from a form. So if information is entered on an ordinary HTML page (with a name and extension like myForm.htm) and is to be processed by a page called myCode.php, the opening form tag should look like this:

<form method="post" action="myCode.php">

But if the posting is to be handled on a form containing the processing code (which will need the extension .php - so it might be called something like myCodedForm.php), the opening form tag should look like this:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Alternatively it might look like this:

<form method="post" action="myCodedForm.php">

My plan is to adapt the examples in this tutorial to the creation of a dummy application, which adds lines to my own database, and to describe my progress here. I shall skip the database creation steps, because that has already been described here. That was for a Derby database, but I also described connecting to a MySQL version with PHP here. I shall focus on the code used to add lines, to view the table contents, and to delete lines.

My progress may be a little slow. I have read a couple of tutorials on JavaScript and PHP, but my practical working knowledge is currently very limited. I shall fiddle away, and post any interesting results as they arise.

1 comment:

Nick Lachey said...

Awesome post. Here’s a tool that lets your build your online database without programming. There is no need to hand code PHP. Cut your development time by 90%
http://www.caspio.com/