Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Klaus Kohlschuetter on Mar 14, 2009 06:03
open dhtmlx forum
Set cell content after request

Hi,

I want to send predefined emails from a list of customers. I create a tabel with colums like customer number, customer name and customer email address. After that I have a butt to start the follwing script:

var request = false;
var rowID;

function TransmitData() {
    
    var file = document.getElementById( "FileInput" );
    var btn = document.getElementById( "SendData" );
    var doctype = document.getElementById( "doctype" );
    
    btn.disabled = true;
    
    if ( file.value == 0 ) {
    
        alert( "Bitte zunächst eine Vorlage auswählen!" );
    
    } else {
        
        for( rowID=0; rowID<mygrid.getRowsNum(); rowID++ ){
    
            mygrid.cells2( rowID, 3 ).setValue( "<img src='./media/working.gif' border='0'>" );
            request = false;

            if ( window.XMLHttpRequest) { // Mozilla, Safari,...
                request = new XMLHttpRequest();
                if (request.overrideMimeType) { request.overrideMimeType('text/xml'); }
            } else if (window.ActiveXObject) { // IE
                try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
                    try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
                }
            }
    
            if (!request) {
                    alert('Giving up:( Cannot create an XMLHTTP instance');
                    return false;
            }
            var url = "http://localhost/ertl/includes/admtool/email.ajax.php?email=kohlschuetter@arcor.de&file="+file.value+"&rowID="+rowID;
            request.onreadystatechange = SetResult;
            request.open( "POST", url, false );
            request.send( null );
}
    }
    return true;
}

function SetResult() {
    if ( request.readyState == 4 ) {
    if ( request.status == 200 || request.status == 304 ) {
            xml = request.responseXML;
            sid_resp = xml.getElementsByTagName("sid")[0];
            sid = sid_resp.firstChild.nodeValue;
            tid_resp = xml.getElementsByTagName("tid")[0];
            tid = tid_resp.firstChild.nodeValue;

            if ( sid == 400 ) {        
                mygrid.cells2( tid, 3 ).setValue( "File not found!" );
                mygrid.setCellTextStyle( tid, 3, "color:red" );                
            } else if ( sid == 120 ) {            
                mygrid.cells2( tid, 3 ).setValue( "FILE ERROR!" );
                mygrid.setCellTextStyle( tid, 3, "color:red" );
            } else if ( sid == 0 ) {            
                mygrid.cells2( tid, 3 ).setValue( "Gesendet!" );    
            }
        } else {    
            mygrid.cells2( tid, 3 ).setValue( "FEHLER!" );
            mygrid.setCellTextStyle( tid, 3, "color:red" );
        }
    }
    return true;
}

What I want to do: 1. Put into a cell the icon <img src='./media/working.gif' border='0'> to show the waiting state
2. After sending the mail switch icon to special text.

My Problem: Everything is running ok, but the icon does'nt appear. After sending all emails, all rows will show the correct text.
Is there a opportunity to refresh the grid in order to make visible the icons?

Thanks so lot.
Klaus
Answer posted on Mar 16, 2009 06:14
>>  request.open( "POST", url, false ); 
Your code is communicating with server in sync. mode, and browser wait until the end of js thread.

To correct behavior you need to use async. mode for data sending ( last parameter as true ) , or break existing loop logic, so each new data sending routine called through setTimeout function
Answer posted by Klaus Kohlschuetter on Mar 16, 2009 10:11
Many thanks for that perfect hint. It seems I do not have understand in detail the difference between "true" and "false".
Answer posted by Klaus Kohlschuetter on Mar 16, 2009 11:03

Hi,

there is onother problem. It works fine snending one email, but when I want to send more than one just the last entry of the table gets the status 4. all others requests are excuted but get no status 4.

How to solve this problem?

Klaus

Answer posted by Support on Mar 17, 2009 10:25
In case of switching to async. mode your previous code must work correctly - it must process all rows , and for each update execute SetResult command, but you are using global variable for all request, so while all request executed in the same time, only one stored in request var and as result only one processed ( each time when xml returned it uses the same response object without relation to actual response )

You can customize existing code or just use dhtmlxAjax ( which already available as part of dhtmlxGrid lib )

//data sending
  for( rowID=0; rowID<mygrid.getRowsNum(); rowID++ ){ 
   
  mygrid.cells2( rowID, 3 ).setValue( "<img src='./media/working.gif' border='0'>" ); 
  var url = "kohlschuetter@arcor.de&file="+file.value+"&rowID="+rowID; '>http://localhost/ertl/includes/admtool/email.ajax.php?email=kohlschuetter@arcor.de&file="+file.value+"&rowID="+rowID; 
  dhtmlxAjax.post(url,null,SetResult); //instead of existing code


function SetResult(loader) { 
  sid_resp = loader.doXPath("//sid")[0]; 
  sid = sid_resp.firstChild.nodeValue; 
  tid_resp = loader.doXPath("//tid")[0]; 
  tid = tid_resp.firstChild.nodeValue; 
....