Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-19-2012, 09:04 PM   PM User | #1
gerble1000
New Coder

 
Join Date: Nov 2011
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
gerble1000 is an unknown quantity at this point
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
gerble1000 is offline   Reply With Quote
Old 01-20-2012, 07:40 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Yes, google for "simple Ajax example"
devnull69 is offline   Reply With Quote
Old 01-21-2012, 04:12 PM   PM User | #3
gerble1000
New Coder

 
Join Date: Nov 2011
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
gerble1000 is an unknown quantity at this point
i cant seem to find an example that does what i need
gerble1000 is offline   Reply With Quote
Old 01-21-2012, 11:11 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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);
devnull69 is offline   Reply With Quote
Old 01-22-2012, 05:52 PM   PM User | #5
gerble1000
New Coder

 
Join Date: Nov 2011
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
gerble1000 is an unknown quantity at this point
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
gerble1000 is offline   Reply With Quote
Old 01-22-2012, 10:42 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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.
devnull69 is offline   Reply With Quote
Old 01-22-2012, 10:52 PM   PM User | #7
gerble1000
New Coder

 
Join Date: Nov 2011
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
gerble1000 is an unknown quantity at this point
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
gerble1000 is offline   Reply With Quote
Old 01-23-2012, 03:31 AM   PM User | #8
Spidey1980
New Coder

 
Join Date: Jan 2012
Posts: 20
Thanks: 2
Thanked 1 Time in 1 Post
Spidey1980 is an unknown quantity at this point
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..
Spidey1980 is offline   Reply With Quote
Old 01-23-2012, 06:59 AM   PM User | #9
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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..
devnull69 is offline   Reply With Quote
Old 01-24-2012, 11:07 PM   PM User | #10
gerble1000
New Coder

 
Join Date: Nov 2011
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
gerble1000 is an unknown quantity at this point
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;
gerble1000 is offline   Reply With Quote
Old 01-25-2012, 11:52 AM   PM User | #11
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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);
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:28 PM.


Advertisement
Log in to turn off these ads.