ajax refreshing problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adrive
    New Member
    • Nov 2007
    • 7

    ajax refreshing problem

    hi guys,

    I'm new to ajax and i'm just following the examples from tizag and w3schools. Apparently, an application i'm trying to build is showing very peculiar behaviour.

    I have a <span> section where it'll actually list out all my subject titles dynamically after a record has been inserted. However, it doesn't always list out all items with the latest added subject.

    BUT

    if i put an alert() before it loads the list, i won't have this problem! What's going on? is alert triggering something ? a state on ajax?

    I'm using oracle 9, IE 7 and Firefox 2.

    below is my ajax code that actually works.

    [PHP]

    function loadSubjectList (){
    try
    {

    var xmlHttp = GetXmlHttpObjec t();
    var url = null;
    var param = null;

    xmlHttp.onready statechange=fun ction()
    {
    if(xmlHttp.read yState==4)
    {
    document.getEle mentById('subje ctList').innerH TML=xmlHttp.res ponseText;
    }
    }

    alert('Ahh!'); //need this to make below refresh 100%

    url = "noteAction.php ";

    xmlHttp.open("P OST", url, true);
    xmlHttp.setRequ estHeader('Cont ent-Type','applicat ion/x-www-form-urlencoded');

    param = 'F_CALL=GETSUBJ ECTLIST';

    xmlHttp.send(pa ram);


    }
    catch(e){
    alert("Failed to load." + e.message);
    }

    }

    [/PHP]
  • adrive
    New Member
    • Nov 2007
    • 7

    #2
    apparently after a deeper research, the problem seems to be even weirder. The sequence of ajax execution is random!

    in a html hyperlink, i'm calling this in sequence :

    insertNewText() ;loadSubjectLis t();


    Below are my codes and output, it contains,

    1.) my insert code
    2.) my display code
    3.) my php code
    4.) my logger output which shows the randomness happening

    ------------------------------------------------------------------------
    1.) my insert code :

    [PHP]
    function insertNewText() {

    if(validate(doc ument.frmNote.t xtSubject, 'Subject')){
    try
    {

    var xmlHttp = GetXmlHttpObjec t();
    var url = null;
    var param = null;

    xmlHttp.onready statechange=fun ction()
    {
    if(xmlHttp.read yState==4)
    {
    document.frmNot e.txtAreaNote.v alue=xmlHttp.re sponseText;
    }
    }

    url = "noteAction.php ";

    xmlHttp.open("P OST", url, true);
    xmlHttp.setRequ estHeader('Cont ent-Type','applicat ion/x-www-form-urlencoded');

    param = 'F_CALL=INSERTN EWTEXT';
    param += '&SUBJECT=' + document.frmNot e.txtSubject.va lue;
    param += '&TEXT=' + document.frmNot e.txtAreaNote.v alue;

    xmlHttp.send(pa ram);

    }
    catch(e){
    alert("Failed to save.");
    }
    }

    }
    [/PHP]

    2. my display code :

    [PHP]

    function loadSubjectList (){
    try
    {

    var xmlHttp = GetXmlHttpObjec t();
    var url = null;
    var param = null;

    xmlHttp.onready statechange=fun ction()
    {
    if(xmlHttp.read yState==4)
    {
    document.getEle mentById('subje ctList').innerH TML=xmlHttp.res ponseText;
    }
    }

    url = "noteAction.php ";

    xmlHttp.open("P OST", url, true);
    xmlHttp.setRequ estHeader('Cont ent-Type','applicat ion/x-www-form-urlencoded');

    param = 'F_CALL=GETSUBJ ECTLIST';

    xmlHttp.send(pa ram);

    }
    catch(e){
    alert("Failed to load." + e.message);
    }

    }
    [/PHP]

    3. my php code :

    [PHP]

    if(isset($_POST['F_CALL'])){

    logger('F_CALL IS SET for ' . $_POST['F_CALL']);

    if($_POST['F_CALL'] == "GETSUBJECTTEXT "){

    echo getSubjectText( );

    logger("get subject's text");

    }else if($_POST['F_CALL'] == "INSERTNEWTEXT" ){

    insertSubjectTe xt();

    logger("insert subject's text");

    }else if($_POST['F_CALL'] == "GETSUBJECTLIST "){

    echo getSubjectList( );

    logger("get subject lists");

    }
    }

    [/PHP]

    4. my output on a logger :

    [22-11-07 03:39:14] F_CALL IS SET for GETSUBJECTLIST
    [22-11-07 03:39:14] F_CALL IS SET for INSERTNEWTEXT
    [22-11-07 03:39:14] get subject lists
    [22-11-07 03:39:14] insert subject's text
    [22-11-07 03:39:21] F_CALL IS SET for GETSUBJECTLIST
    [22-11-07 03:39:21] F_CALL IS SET for INSERTNEWTEXT
    [22-11-07 03:39:21] insert subject's text
    [22-11-07 03:39:21] get subject lists
    [22-11-07 03:39:22] F_CALL IS SET for INSERTNEWTEXT
    [22-11-07 03:39:22] F_CALL IS SET for GETSUBJECTLIST
    [22-11-07 03:39:22] get subject lists
    [22-11-07 03:39:22] insert subject's text
    [22-11-07 03:39:22] F_CALL IS SET for INSERTNEWTEXT
    [22-11-07 03:39:22] F_CALL IS SET for GETSUBJECTLIST
    [22-11-07 03:39:23] insert subject's text
    [22-11-07 03:39:23] get subject lists
    [22-11-07 03:39:33] F_CALL IS SET for INSERTNEWTEXT
    [22-11-07 03:39:33] F_CALL IS SET for GETSUBJECTLIST
    [22-11-07 03:39:33] insert subject's text
    [22-11-07 03:39:33] get subject lists
    [22-11-07 03:39:34] F_CALL IS SET for GETSUBJECTLIST
    [22-11-07 03:39:34] F_CALL IS SET for INSERTNEWTEXT
    [22-11-07 03:39:34] insert subject's text
    [22-11-07 03:39:34] get subject lists

    Comment

    • adrive
      New Member
      • Nov 2007
      • 7

      #3
      anyone? or perhaps this might be due to multi request calls?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        If you want this in sequence, you will have to wait until the first request is complete before making the second request.

        Comment

        • adrive
          New Member
          • Nov 2007
          • 7

          #5
          Originally posted by acoder
          If you want this in sequence, you will have to wait until the first request is complete before making the second request.
          hi, how do i check if the first request was completed?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by adrive
            hi, how do i check if the first request was completed?
            When readyState is 4. You could make the second request in the onreadystatecha nge-called function when the first request's readyState is 4.

            Comment

            • adrive
              New Member
              • Nov 2007
              • 7

              #7
              thanks acoder, it now works :)

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                You're welcome. Glad you got it working.

                Comment

                Working...