count down timer - JavaScript inside Ajax not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • canabatz
    New Member
    • Oct 2008
    • 155

    count down timer - JavaScript inside Ajax not working

    Hi all!

    i got a countdown timer for auctions that runing.

    i got a DIV that refreshs every 4 seconds
    that DIV is refreshed by ajax ,the file that is refreshed is timer.php for example!!

    my problem is that the timer.php is not showing up.
    if i run the timer.php alone it is showing the timer ,the problem i think it's because the timer sits inside the ajax DIV and that is what i think making me the problem!!

    any help please?


    thanx :)
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    There is really not any way to make any suggestions if you do not post the relevant code you are asking about.

    Comment

    • canabatz
      New Member
      • Oct 2008
      • 155

      #3
      i think i know what the problem is ,but i need help

      my problem is that the javascript return from ajax is inside php file

      the javascript is executed in php so the problem is the ECHO javascript ,that closeing the response!!

      here is the code:

      Code:
      echo "<div id='$bid_id'></div>
      <script type=\"text/javascript\">
      var launchdate2=new cdLocalTime(\"$bid_id\", \"server-php\", 0, \"$exmonth $expdate[2], $expdate[0] ";
      if($expiretime=='0')
      	{echo "00:00:00";}
      else { echo $expiretime;}
      echo "\")
      launchdate2.displaycountdown(\"\", formatresults2)
      </script>";
      }   
      else // Show some other content
              {
      		echo "<font color='#FF0000' size='3''>you can still bid</font>";
      
      
      		}
      ?>
      the last echo is working if there is no time left ,if there is time left then it showing nothing.

      i need help taking this javascript out of the php.

      please help me with that!

      thanx

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You need to either eval the JavaScript or put the JavaScript code separately into the head of the calling page.

        Comment

        • canabatz
          New Member
          • Oct 2008
          • 155

          #5
          this is what i want ,but dont know how

          i need the javascript to be where it should be between the php script.

          i dont understand all those slashes :(

          any help with that?

          thanx

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Either have two separate requests: one for the HTML and the other for the JavaScript code; or parse the response to get the script to either eval or append to the head of the parent document. You don't necessarily need it where you currently have it because the ID should be enough for it to find where the timer should be displayed.

            Comment

            • canabatz
              New Member
              • Oct 2008
              • 155

              #7
              im not expert :(

              my problem is the ajax that look for the response from the div,
              now the javascript is executed by echo ,so the ajax is taking just the script as respond

              please help me take the javascript from the echo, there im stuck :(

              thanx

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                If we take the split route, move lines 3-8 into their own mini script and call that separately. Then you can just eval the response.

                Comment

                • canabatz
                  New Member
                  • Oct 2008
                  • 155

                  #9
                  ok ,i did the script out of the php ,but no response

                  here is the ajax code im using
                  Code:
                  <script type="text/javascript">
                  function getXMLHttp()
                  {
                    
                  var xmlHttp;
                   
                    try
                    {
                      //Firefox, Opera 8.0+, Safari
                      xmlHttp = new XMLHttpRequest();
                    }
                    catch(e)
                    {
                      //Internet Explorer
                      try
                      {
                        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                      }
                      catch(e)
                      {
                        try
                        {
                          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch(e)
                        {
                          alert("Your browser does not support AJAX!")
                          return false;
                        }
                      }
                    }
                    return xmlHttp;
                  }
                  
                   var xmlHttp;
                  
                  function MakeRequest2()
                  {
                    var xmlHttp = getXMLHttp();
                    
                    xmlHttp.onreadystatechange = function()
                    {
                      if(xmlHttp.readyState == 4)
                      {
                        HandleResponse(xmlHttp.responseText);
                      }
                    }
                  nocache = Math.random();
                    xmlHttp.open("GET", "check2.php?nocache="+nocache); 
                    xmlHttp.send(null);
                   setTimeout('MakeRequest2()', 4000);
                  }
                  
                  function MakeRequests()
                  {
                    var xmlHttp = getXMLHttp();
                    
                    xmlHttp.onreadystatechange = function()
                    {
                      if(xmlHttp.readyState == 4) 
                  	    
                      {  
                        HandleResponsec(xmlHttp.responseText);
                      }
                    }
                  nocache = Math.random();
                    xmlHttp.open("GET", "main.php?nocache="+nocache, true); 
                    xmlHttp.send(null);
                  }
                  
                  function HandleResponsec(res)
                  {
                    document.getElementById('content').innerHTML = res;
                  }
                  
                  
                  function HandleResponse(response)
                  {
                    if (document.getElementById('id').innerHTML == response)
                      {
                    
                   
                  
                      }
                  
                  else if (document.getElementById('id').innerHTML = response)
                  
                      {
                  MakeRequests();
                  
                      }
                  	
                  
                  
                  }
                  
                  
                  window.onload=MakeRequest2();
                  	</script>
                  the "content" div is empty if there is time left ,if there is no time left ,it writing me that there is no more time ,it's like the ajax is refusing to display the timer :)

                  what can be wrong with this?

                  thanx

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    For the JavaScript, you will need to eval, e.g.
                    Code:
                    eval(xmlHttp.responseText);
                    but this would be if the response is pure JavaScript. Alternatively, you can append to the head using something like:
                    Code:
                    var script = document.createElement("script");
                    script.type="text/javascript";
                    var txt = document.createTextNode(xmlHttp.responseText);
                    script.appendChild(txt);
                    document.getElementsByTagName("head")[0].appendChild(script);
                    This would be if you only had one line. If you return more than one line, you will need to split them up by newline and append the text nodes one by one.

                    Comment

                    • canabatz
                      New Member
                      • Oct 2008
                      • 155

                      #11
                      Acoder thanx

                      like i said the main.php works fine if i run it alone ,i did succeed to take the javascript out of the php ,but it didnt change any thing !!

                      the problem is in the ajax script.


                      it's not displaying the timer ,it's just displaying if there is no time left!

                      thanx

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        What does check2.php do? You've missed the third parameter in open() and in the callback function HandleResponse( ), the else is setting the innerHTML to the response.

                        Comment

                        • canabatz
                          New Member
                          • Oct 2008
                          • 155

                          #13
                          check2.php

                          this is a short check to the database to see if there is change in some thing.

                          if there is changes it will call the next call ,the main.php to refresh the results.

                          the main.php is sitting inside a DIV in my index.php

                          can you point me where there is something wrong in my ajax script?

                          thanx ;)

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            So, which one contains the JavaScript code? main.php? Have you got a test page that I can have a look at?

                            Comment

                            • canabatz
                              New Member
                              • Oct 2008
                              • 155

                              #15
                              in index.php head ,there is the src of javascript.js

                              in javascript.js there is the call to two php files

                              check2.php checks for changes in the data base ,if there is changes it refeshes
                              the main.php that sit in the content DIV

                              áåð÷ø îëøæéí ,its the file im runing inside the ajax DIV that dont show me the count down timer. if i run this file alone it showing me the timer ,if it's inside the div it's not showing me!!

                              áåð÷ø îëøæéí it's my testing page that not showing me the timer.

                              thanx

                              Comment

                              Working...