Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts

    hi guys need help with a simple ajax script please

    i need the simplest way to add to my javascript code a way of getting data from a text file in the same directory as the index.html and putting the data into a variable called data1 to be used by the rest of the script
    any ideas

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Yes, google for "simple Ajax example"

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i cant seem to find an example that does what i need

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    i cant seem to believe that

    This is what came up as the very first result googling for Ajax example
    Code:
    var myvar = "";
    var http = XMLHttpRequest();
    http.open('GET', 'datatextfile.txt', true);
    http.onreadystatechange = function() {
       if(http.readyState == 4 && http.status == 200)
           myvar = http.responseText;
    }
    http.send(null);

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ok thanx
    i have played with this and i am finding it a problem...here is my code

    Code:
    <script type="text/javascript">
    function changecolor()
    {
    var myvar = "";
    
    	var http = createRequestObject();
    
    	function createRequestObjecte() {
    		var objAjax;
    		var browser = navigator.appName;
    		if (browser == "Microsoft Internet Explorer"){
    			objAjax = new ActiveXObject("Microsoft.XMLHTTP");
    		}else{
    			objAjax = new XMLHttpRequest();
    		}
    
    
    		http.open('get',text1.txt,true);
    		http.send(null);
    		myvar = http.responseText; 
    
    	}
    
    
    
    
    
    
    
    
    
    var tcolor1 = myvar;
    var tcolor2 = "pink";
    var tcolor3 = "blue";
    var tcolor4 = "orange";
    
    
    
    
    
    document.getElementById('color1').style.color = tcolor1;
    document.getElementById('color2').style.color = tcolor1;
    document.getElementById('color3').style.color = tcolor1;
    document.getElementById('color4').style.color = tcolor1;
    document.getElementById('color5').style.color = tcolor1;
    document.getElementById('color6').style.color = tcolor1;
    document.getElementById('color7').style.color = tcolor1;
    document.getElementById('color8').style.color = tcolor1;
    }
    </script>

    could you please tell me what i am doing wrong

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Yes, you did not use my example above. Instead you removed the (important) callback onreadystatechange and treated the responseText from an asynchronous request (third parameter of open is true) as if it was a synchronous one ... which obviously doesn't work. Additionally, starting from IE7 you do not need a browser switch any more as every modern browser recognizes the XMLHttpRequest object.

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i know i dont know as much as you but i am trying my best...
    im more familiar with perl and c++.. could you please point out what i have done wrong and please help correct.
    i supply people with information on another forum for help with other languages.
    could you be a professional and help please

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    20
    Thanks
    2
    Thanked 1 Time in 1 Post
    http://bookofjavascript.com/Chapter16/Fig16-15.txt HTML that uses the following PHP
    http://bookofjavascript.com/Chapter16/readTextFile.txt PHP file that opens and reads files
    Last edited by Spidey1980; 01-23-2012 at 03:34 AM.

  • #9
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Please try to adhere as close as possible to my first example ... and if it doesn't work, please come back with your rewritten code. Then we will find the problem.

    The goal of this forum is not to write the code for you but to guide you to help yourself as good as possible.

    Code:
    var myvar = "";
    var http = XMLHttpRequest();
    http.open('GET', 'datatextfile.txt', true);
    http.onreadystatechange = function() {
       if(http.readyState == 4 && http.status == 200)
           myvar = http.responseText;
    }
    http.send(null);
    Hint: The first A in Ajax means "asynchronous". I think coming from C++ you should be aware of this construct. Essentially it means that the normal Javascript program flow continues while "some background task" is still running. As soon as the background task triggers some event (in this case the change of the readyState), it will call a so-called "callback function" (onreadystatechange). readyState==4 says: Request complete, status==200 says HTTP OK. In that case you can grab the PHP script output from responseText and do with it whatever you want
    Last edited by devnull69; 01-23-2012 at 07:01 AM.

  • #10
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i dont get it im racking my brains for days now and still cant get this to work

    here is what i have

    Code:
    <script type="text/javascript">
    function changecolor()
    
    
    
    
    
    {
    var myvar = "";
    var http = XMLHttpRequest();
    http.open('GET', 'text1.txt', true);
    http.onreadystatechange = function() {
       if(http.readyState == 4 && http.status == 200)
           myvar = http.responseText;
    }
    http.send(null);
    
    
    
    
    
    
    
    
    var tcolor1 = myvar;
    var tcolor2 = "pink";
    var tcolor3 = "blue";
    var tcolor4 = "orange";
    
    
    
    
    
    document.getElementById('color1').style.color = tcolor1;
    document.getElementById('color2').style.color = tcolor1;
    document.getElementById('color3').style.color = tcolor1;
    document.getElementById('color4').style.color = tcolor1;
    document.getElementById('color5').style.color = tcolor1;
    document.getElementById('color6').style.color = tcolor1;
    document.getElementById('color7').style.color = tcolor1;
    document.getElementById('color8').style.color = tcolor1;

  • #11
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    This is a common mistake. Outside of onreadystatechange the "myvar" variable remains undefined, because this code will be executed BEFORE the request has finished.

    Conclusion: Everything you want to do depending on the result of the request has to be (started from) inside the onreadystatechange callback

    Code:
    var myvar = "";
    var http = XMLHttpRequest();
    http.open('GET', 'text1.txt', true);
    http.onreadystatechange = function() {
       if(http.readyState == 4 && http.status == 200) {
           myvar = http.responseText;
           var tcolor1 = myvar;
           var tcolor2 = "pink";
           var tcolor3 = "blue";
           var tcolor4 = "orange";
           document.getElementById('color1').style.color = tcolor1;
           document.getElementById('color2').style.color = tcolor1;
           document.getElementById('color3').style.color = tcolor1;
           document.getElementById('color4').style.color = tcolor1;
           document.getElementById('color5').style.color = tcolor1;
           document.getElementById('color6').style.color = tcolor1;
           document.getElementById('color7').style.color = tcolor1;
           document.getElementById('color8').style.color = tcolor1;
       }
    }
    http.send(null);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •