Posts

Showing posts from September, 2011

Applet to AJAX to PHP methodology

I have an Applet, which adds lines to a database one at a time, by sending an SQL string via AJAX to a PHP script. The Applet data insertion method is as follows: private void addItem3(String newWord) { if(LIVE) { if(jso != null ) try { jso.call("updateWebPage", new String[] {newWord}); } catch (Exception ex) { addItem2("jso call failed... "); ex.printStackTrace(); } } } Where the parameter newWord being passed to that method might look like: INSERT INTO mytable (Partid, OpCode, ItemLeft, ItemRight, Raw, Rate) VALUES (684, 1, 2, 5, 1, 34) The JavaScript function updateWebPage and the PHP script were given in my previous post , so I won't repeat them here. The thing is, even a single instance of the Applet can generate new lines very quickly, sometimes once a second. So a classroom of 25 students might generate 25 lines a second. How will my ISP server react to all these requests to open a connection, insert a single line of data, and close the c

Applet to JavaScript to PHP post to Database

Image
This post brings together the previous two. In the first , I called PHP code from a JavaScript function using an AJAX command. In the second , I passed an SQL command to a JavaScript function. In this post I describe bringing the two together. Instead of simply displaying the SQL in a text box on the web page, passing it to a PHP file. The applet code was unchanged, but the JavaScript function was extended from: <script type="text/javascript"> function updateWebPage(myArg) { document.getElementById("txt1").innerHTML=myArg; } </script> to: <script type="text/javascript"> function updateWebPage(myArg) { document.getElementById("txt1").innerHTML=myArg; if (myArg=="") { document.getElementById("cbxItem").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObje

Pass SQL command from Applet to JavaScript function

Image
My last post recorded an important milestone for me, because I managed to call PHP code from a JavaScript function. On an earlier occasion, I had managed to call a JavaScript function from an Applet. My next task was to put the two together, and I did this in two steps the first of which (reported in the current post) was to adapt the previous Applet to JavaScript example to display an SQL command. This was essentially a cosmetic exercise, but it acted as a refresher on the code. The only change to the Applet code was to replace "Hello World" with an SQL string. There is no need to reprint that here as it was given in my previous post on the topic. In the HTML, I modified the table to display the Applet above the text box, and I widened them both. I also removed the update Applet function, as it was not required for the current exercise. It was then as follows: <html> <body> <script type="text/javascript"> function updateWeb

PHP and JavaScript/AJAX database query

Image
Including the term JavaScript here is tautologous really because AJAX written out in full is Asynchronous JavaScript and XML. But as combining PHP and JavaScript has become something of a holy grail for me, I thought I'd include it anyway. In fact my September 9 blog entry was originally posted under the title PHP and JavaScript (so keen was I to use it), until I reflected on the text overnight and realised there was actually nothing about JavaScript in the post. I have been running around in circles a bit over the last couple of months, but they have been good circles, because they have enriched my understanding of a number of web related topics. And my current circle has taken me back to the generally excellent and easy to follow W3Schools website. In my post on their AJAX tutorial, I bemoaned that fact that their exchanging data with a server example only read data from a "measly old text file", rather than a database. However, had I dug deeper i

Simple Web Application – Delete Data line

Image
Once again I should like to acknowledge the Virginia Tech Simple Web Application sample code, which provided a template for this little project, which has really helped to improve my practical understanding of PHP. Having said that, the code, required to delete lines from the database, is pretty slim. Once again I bloated it out with some diagnostic HTML, just to check the query. I hate running blind. The bloated code was as follows: <?php $id = $_GET['id']; if (empty($id)) { Header("Location: listcontacts.php"); exit; } include('dbinfo.php');// collect database variables and connect. $con = mysql_connect( $dbhost, $dbuser, $dbpass ); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $con); $query = "DELETE FROM mytable WHERE Itemid=".$id; $result = mysql_query($query) or die(mysql_error()); mysql_close($con); //close connection because job finished // Header("Location: jsphp4.php"); //r

Simple Web Application – Add Data Form

Image
While I must still acknowledge the Virginia Tech Simple Web Application sample code, at this stage in the project I was primarily cutting and pasting from the code shown in my previous posts , and manually making a few small changes from the sample code. The code used in the Add data line form is very similar to that for the Edit data line form, except that there is no existing data to display, and the SQL command is INSERT rather than UPDATE. I also removed the input validation code. Any data entered manually into my database is bogus and must be deleted after this exercise has finished, so it really doesn't matter what fields are filled or left empty. With no data to display, there no need to confirm that a data line was being correctly passed from the main display form, and no need to confirm that the line was being correctly read. I therefore left in just a couple of diagnostic messages at the top of the form: <p>The query is: <?php echo $query ?>

Simple Web Application – Data edit form

Image
The Virginia Tech Simple Web Application sample code works perfectly on their server, but as I reported in my previous post, getting something similar to run with my own database has been a fairly long and frustrating task. I said in my last post that I constructed my own PHP PEAR database layer , as a result of which, my code and the sample code diverged from an early point. Specifically: include('dbconnect.php'); was stretched out to: include('dbinfo.php'); $con = mysql_connect( $dbhost, $dbuser, $dbpass ); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $con); I had some specific problems with certain lines of the sample code. One was: $result = $database->query($query); I didn’t understand the funny little -> in this line (I think it might have something to do with Perl, which takes it beyond the scope of this post), nor the general syntax, so I replaced it with: $result = mysql_query($query) or di

Simple Web Application – Data Display page

Image
I can read tutorials 'til I'm blue in the face, but unless I try to adapt the sample code to do something completely different, I don't retain anything. The live " Try it yourself " windows are quite fun, but unless I can get code running on my own or my ISP's server, I can't expose my misunderstandings or the gaps in my knowledge. The Virginia Tech simple web application sample code works perfectly on their server, but recreating something to perform similar functions with my own database on my own server has been a fairly long and frustrating job. I said in my last post that I could not see any gaps in the sample code. On closer inspection, I have found a couple, of which the most important was their connection code. They refer to a PHP PEAR database layer in the text and a file called DB.php in their code, but they don’t show what is in the file. So I followed the PHP PEAR link and constructed my own file based on the template given there

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

Applet to JavaScript Communication

Image
The raditha.com site, to which I referred in my Javascript to Applet Communication - Example 2 entry, also includes a page on Applet to JavaScript Communication , but I could not find the required HTML/JavaScript, either on the page or in the code files included at the end of the page. This was disappointing because overall it is the best site I have found on what Mozilla calls LiveConnect . The Java code works, and I shall cite a simplified (less graphics) version here: import javax.swing.*; import netscape.javascript.JSObject; import java.awt.*; import java.awt.event.*; public class JSHelloWorld2 extends JApplet { JTextArea txt = new JTextArea(100,100); JButton btn = new JButton("Update Page"); JSObject jso; public JSHelloWorld2() { txt.setText("Hello World"); getContentPane().setLayout(new BorderLayout()); getContentPane().add(txt); getContentPane().add(btn,BorderLayout.SOUTH); btn.addActionListener(new ActionListener() { public void actionPerformed

LiveConnect and JavaScript Wrappers

From the LiveConnect page on the Mozilla web site : LiveConnect is the name of an application programming interface that provides JavaScript with the ability to call methods of Java classes and vice-versa using the existing Java infrastructure. LiveConnect use by applets is enabled via the use of the "MAYSCRIPT" attribute in applet tags on an HTML page, following which the applet may refer to classes in the netscape.javascript package to access Javascript objects, and scripts may directly call applet methods (using the syntax document.applets.name.methodName() ). From the LiveConnect Overview page on the same site: In JavaScript, a wrapper is an object of the target language data type that encloses an object of the source language. When programming in JavaScript, you can use a wrapper object to access methods and fields of the Java object; calling a method or accessing a property on the wrapper results in a call on the Java object. On the Java s

Javascript to Applet Communication - Example 2

Image
I have found another article on this topic where the code works and I shall cite it here because the Java code is simpler and therefore perhaps more elegant than the previous one . import javax.swing.*; import netscape.javascript.JSObject; public class JSHelloWorld extends JApplet { JTextArea txt = new JTextArea(100,100); public JSHelloWorld() { txt.setText("Hello World"); getContentPane().add(txt); } public void setText(String s) { txt.setText(s); } } The HTML, on the other hand is a bit more wordy, because the Applet and the JavaScript form are laid out side by side, but I like it: <html> <body> <script type="text/javascript"> function updateApplet() { document.jsap.setText(document.forms[0].txt1.value); } </script> <form> <table border=0 align='center' cellpadding=0 cellspacing=0 > <tr><td valign='top' width=180> <table> <tr><td style='text-ali

Java Applet/Javascript Communication

Image
So I have established that for the last decade, not many people have used Applets in web pages. The resulting paucity tutorials or recent examples made it very difficult even to scratch the surface of how to do this. I know when I write notes here, I often skip steps, which seem too obvious or easy, so that when I revisit an entry written two years ago, even I find it hard to follow. So I can't criticize others for being cryptic, or telling only half the story. But it has been frustrating wading through articles with gaps, or code that doesn't work. But where to begin with an attempt at a full story? I think a revisit to paths and class paths. In order to compile and run java classes, you need to set a path to the JDK binaries folder. This can be done once at the start of a command window session, or even as a Windows environment variable. In order to run classes which connect to a database, you need set a "class" path to a special .jar package co