[thelist] XMLhttpRequest, javascript arrays?

Courtenay court3nay at gmail.com
Fri Mar 18 01:53:27 CST 2005

Hi all,

I'm (bravely) building a data-entry app (in Rails, heh) that uses
xmlHttpRequest to send the form data asynchronously to the server (it
also degrades without javascript).

The code works something like this:

 -Data is in a table with 3 columns
- User enters data in a form field at the end of the table
- Javascript intercepts form.onsubmit, appends a new row to the
existing data with the text from the form.
- Javascript sends xmlHttpRequest to a controller, which adds it to the db.

So the process is quite fast for the user (immediate in fact) and
there's no waiting around for the database (inserts being quite slow).
 If there's an error, it uses a css popup to alert the user.  If the
insert is successful, it changes a little icon next to the item.

[x] [   Item name               ] [ $4.50 ]
[x] [  Other  Item name      ] [ $3.25 ]
[  ] [  New item name         ] [ $2.50 ]

     [  (form field                 ] [ (form field) ]   [Submit]

Here's the problem: I store the 'current row' as a global var (eek). 
If the user adds a few rows quickly, it can't keep up with the icon


var req;
var cur;
function XMLreq(url, target) {
  cur = target;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  ... etc.

function processReqChange() {
  if (req.readyState == 4 && req.status == 200) {
   cur.innerHTML += req.responseText;

So maybe if I use associative arrays like
 cur[url] = target;

then on processReqChange use
 cur[url].innerHTML = req.responseText

However, there's no way of retrieving the URL from the xmlHTTPrequest 
data... or is there?

Any other suggestions?  Another list to post to?



More information about the thelist mailing list