Jump to content

got the request


user4fun

Recommended Posts

Tjis orignal post was edited becuase i guess i did not explain it righthere is what i haveI need to send the var UsingAs to file run_uc.php ( nothing will be expected to return). I cannot use the open line because there is already code on there that opens a different php file, i need to use the send () to send the content of the cookie variable as delcared in the global variable section. ("UsingAs")

var UsingAs = getCookie('website')function GetInfo(t) 	var xmlHttp;	   try		 //more code					 //more code			xmlHttp.onreadystatechange=function()				 {			 if(xmlHttp.readyState==4) 					  {	   // more code				  }		  }		   xmlHttp.open(//more code);		   xmlHttp.send("[b]NEED HELP HERE[/b]");	 }}</script>

Link to comment
Share on other sites

Why don't you just read the cookie value from the PHP page..?But if you want to do it this way, then you could do something like:

xmlHttp.open("GET", "run_uc.php?usingas=" + UsingAs, true);xmlHttp.send(null);

And the run_uc.php page will read the var as $_GET['usingas']

Link to comment
Share on other sites

Synook had it right.If you want to send the parameter to the php page with a GET request:

xmlHttp.open("GET", "run_uc.php?usingas=" + UsingAs, true);xmlHttp.send("");

Or, if you want to send the parameter to the php page with a POST request:

var data = "usingas=" + UsingAs;xmlHttp.open("POST", "run_uc.php", true);// setting the request header may be optional...xmlHttp.setRequestHeader("Content-Length", data.length);xmlHttp.send(data);

Link to comment
Share on other sites

OK, maybe i dont understand what is oging onthe xmlHttp.open already HAS code in it?? I need it to do something else 2cani have two of them???here is the entire code,I need to ask file get_info.php to give me some data and i ned to send the var Using As to file run_uc.phpThey both ahppen every time in the loop. if i cannot send UsingAs variabvle then at least run the file run_uc.php and not expect any thing back, in that case run_uc.php will get the variable form the original cookiehere is the entire code

<script type="text/javascript">var UsingAs = getCookie('website')function GetInfo(t) {	 if (t <= 100)			{		  var xmlHttp;		   try			 {		   xmlHttp=new XMLHttpRequest();			 }		   catch (e)		{		   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;			 }		   }		   }	xmlHttp.onreadystatechange=function()				 {			 if(xmlHttp.readyState==4) 					  {		picked_file=window.open("http://" + xmlHttp.responseText,"picked_file","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=200");		t++;		setTimeout("picked_file.close(); GetInfo(" + t + ");",9000);		  }		  }			   xmlHttp.open("POST","get_info.php",true);										xmlHttp.send("POST", "run_uc.php?usingas=" + UsingAs, true);					 }}</script>

Link to comment
Share on other sites

The way this works is you create the XMLHttpRequest object first, that is the Javascript object that sends the request and gets a response back. Before you use it, you set a function handler to get executed when the response comes back. That's what this does:xmlHttp.onreadystatechange=function()That defines a function with no name to get executed when the readystate changes. Whenever the readystate changes, that function gets executed. If you want to send more then one request, you will probably want to define more then one function handler, so that something different happens when the different responses come back.You can call the open and send methods as many times as you want, but as it is now even if you send requests to 2 different pages, when each response comes back it will be processed by the same function, the function that opens a new window. You will want to set up multiple functions to handle your different responses, and then send each request individually after you set the appropriate handler for when the response comes back.Also, you only need to create the XMLHttpRequest object once, it's a waste of resources and time to re-create it every time, might as well just create it once and re-use it. Look at this code:

<script type="text/javascript">var UsingAs = getCookie('website');var xmlHttp;var t = 0;try{  xmlHttp=new XMLHttpRequest();}catch (e){  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;	}  }}function resp_openwin(){  if(xmlHttp.readyState==4)  {	picked_file=window.open("http://" + xmlHttp.responseText,"picked_file","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=200");	t++;	setTimeout("picked_file.close(); GetInfo();",9000);	//once this response comes back, we can send the second request	updateData();  }}function resp_ignore(){  return;}function GetInfo(){  if (t <= 100)  {	//set the handler and send the first request	xmlHttp.onreadystatechange = resp_openwin;	xmlHttp.open("POST","get_info.php",true);	xmlHttp.send(null);  }}function updateData(){  //set the handler and send the second request  xmlHttp.onreadystatechange = resp_ignore;  xmlHttp.open("POST", "run_uc.php?usingas=" + UsingAs, true);  xmlHttp.send(null);}</script>

The first thing it does is create the AJAX object, then it defines 4 functions. The first two functions (resp_openwin and resp_ignore) are the function handlers for when the responses come back. resp_openwin opens the window, and resp_ignore doesn't do anything. You can change and rename resp_ignore if you want something to happen when the second response comes back. Then the GetInfo and updateData functions are the two functions that send the requests out. GetInfo is the one you already had and sends the request to get the link, and updateData sends the request to the run_uc page. Notice that all the functions have to do is set the response handler and send the request, they don't need to re-create the AJAX object or whatever else.Also, a key thing to keep in mind is that the A in AJAX stands for asynchronous, so you can't just send one request, re-set the response handler, and send the next request. Asynchronous means the program doesn't stop and wait for the function to finish. Javascript will continue running while it is still waiting for the response to come back. So if you just do this:

//set the handler and send the first requestxmlHttp.onreadystatechange = resp_openwin;xmlHttp.open("POST","get_info.php",true);xmlHttp.send(null);//set the handler and send the second requestxmlHttp.onreadystatechange = resp_ignore;xmlHttp.open("POST", "run_uc.php?usingas=" + UsingAs, true);xmlHttp.send(null);

Then by the time the first response comes back you will already have change the request handler so both responses will go to the same handler. In order to get around that you need to send the request after you have received the first response, so the function call to updateData to send the second request is in the response handler for the first request.

Link to comment
Share on other sites

here is what it has become, it is only asking for the get_info.php file once?? one window, one close nd stops :-(

<script type="text/javascript">var UsingAs = "test"var xmlHttp;var t = 0;try{  xmlHttp=new XMLHttpRequest();}catch (e){  try  {	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  }  catch (e)  {	try	{	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	}	catch (e)	{	  alert("Your browser does not support AJAX!");	 }  }}function resp_openwin(){  if(xmlHttp.readyState==4)  {	picked_file=window.open("http://" + xmlHttp.responseText,"picked_file","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=200");	t++;	setTimeout("picked_file.close(); GetInfo();",9000);	updateData();  }}function resp_ignore(){  return;}function GetInfo(t){  if (t <= 100)  {		xmlHttp.onreadystatechange = resp_openwin;	xmlHttp.open("POST","get_info.php",true);	xmlHttp.send(null);  }}function updateData(){    xmlHttp.onreadystatechange = resp_ignore;  xmlHttp.open("POST", "run_uc.php?usingas=" + UsingAs, true);  xmlHttp.send(null);}</script><?//some php code?><form name="MyUCIC">  <input type="button" onClick="GetInfo(t);" name="start" value="Start" tabindex="5" /><br>  <input type="button" onClick="GetInfo(100);" name="stop" value="Stop" tabindex="6" /></form>

Link to comment
Share on other sites

You added a parameter to getinfo. Remove the t parameter from getinfo, you don't need it. t is a global variable that gets updated whenever the request comes back. The code is failing because there is a call to getinfo with no parameter. Look at what happens when you call this function:GetInfo(100);

function GetInfo(t){  if (t <= 100)  {		xmlHttp.onreadystatechange = resp_openwin;	xmlHttp.open("POST","get_info.php",true);	xmlHttp.send(null);  }}

It doesn't do anything at all. You don't need to call getinfo with a value larger then 99, it will not affect anything if you do. If you want to stop the loop, you need to clear the timeout that you set earlier, that is the correct way to stop a timeout. In order to do that, you need a global timeout variable so you can access it in more then one function.

<script type="text/javascript">var UsingAs = document.cookie('website');var xmlHttp;var t = 0;var timeout;try{  xmlHttp=new XMLHttpRequest();}catch (e){  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;	}  }}function resp_openwin(){  if(xmlHttp.readyState==4)  {	picked_file=window.open("http://" + xmlHttp.responseText,"picked_file","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=200");	t++;	timeout = setTimeout("picked_file.close(); GetInfo();",9000);	//once this response comes back, we can send the second request	updateData();  }}function resp_ignore(){  return;}function GetInfo(){  if (t <= 100)  {	//set the handler and send the first request	xmlHttp.onreadystatechange = resp_openwin;	xmlHttp.open("POST","get_info.php",true);	xmlHttp.send(null);  }}function updateData(){  //set the handler and send the second request  xmlHttp.onreadystatechange = resp_ignore;  xmlHttp.open("POST", "run_uc.php?usingas=" + UsingAs, true);  xmlHttp.send(null);}</script>

And your buttons can be like this: <input type="button" onclick="GetInfo();" name="start" value="Start" tabindex="5" /><br> <input type="button" onclick="clearTimeout(timeout);" name="stop" value="Stop" tabindex="6" />

Link to comment
Share on other sites

I am so sick of this, justsomeguy, if it was not for you i would have quit a long time ago,i am still getting error on page. here is the full thing, what am i missing.

<html><head><?$website = "testing";?><script type="text/javascript">var UsingAs ="<?=$website;?>";var xmlHttp;var t = 0;var timeout;try{  xmlHttp=new XMLHttpRequest();}catch (e){  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;	}  }}function resp_openwin(){  if(xmlHttp.readyState==4)  {	picked_file=window.open("http://" + xmlHttp.responseText,"picked_file","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=200");	t++;	timeout = setTimeout("picked_file.close(); GetInfo();",9000);	updateData();  }}function resp_ignore(){  return;}function GetInfo(){  if (t <= 100)  {	xmlHttp.onreadystatechange = resp_openwin;	xmlHttp.open("POST","get_info.php",true);	xmlHttp.send(null);  }}function updateData(){  xmlHttp.onreadystatechange = resp_ignore;  xmlHttp.open("POST", "run_uc.php?usingas=" + UsingAs, true);  xmlHttp.send(null);}</script></head><body><?include ("../inc/main_hdr.php");?><form name="MyUCIC"><input type="button" onclick="GetInfo();" name="start" value="Start" tabindex="5" /><br><input type="button" onclick="clearTimeout(timeout);" name="stop" value="Stop" tabindex="6" /> </form>						 <?include ("../inc/main_btm.php");?></body></html>

Link to comment
Share on other sites

Hmm.. this works fine in Opera and Firefox but not in IE.

<html><head><?$website = "testing";?><script type="text/javascript">var UsingAs ="<?=$website;?>";var xmlHttp;var t = 0;var timeout;var picked_file;try{  xmlHttp=new XMLHttpRequest();}catch (e){  try  {	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  }  catch (e)  {	try	{	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	}	catch (e)	{	  alert("Your browser does not support AJAX!");	}  }}function resp_openwin(){  if(xmlHttp.readyState==4)  {	document.getElementById("msg").innerHTML += "response received, opening window<br>";	picked_file=window.open("http://" + xmlHttp.responseText,"picked_file","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=200");	t++;	timeout = setTimeout("picked_file.close(); GetInfo();",3000);	updateData();  }}function playitagain(){  picked_file.close();  GetInfo();}function resp_ignore(){  document.getElementById("msg").innerHTML += "response received, ignoring<br>";  return;}function GetInfo(){  if (t <= 100)  {	document.getElementById("msg").innerHTML += "sending request for GetInfo...<br>";	xmlHttp.onreadystatechange = resp_openwin;	xmlHttp.open("POST","get_info.php",true);	xmlHttp.send(null);  }}function updateData(){  document.getElementById("msg").innerHTML += "sending request for updateData...<br>";  xmlHttp.onreadystatechange = resp_ignore;  xmlHttp.open("POST", "run_uc.php?usingas=" + UsingAs, true);  xmlHttp.send(null);}</script></head><body><?#include ("../inc/main_hdr.php");?><form name="MyUCIC"><input type="button" onclick="GetInfo();" name="start" value="Start" tabindex="5" /><br><input type="button" onclick="clearTimeout(timeout);" name="stop" value="Stop" tabindex="6" /></form><?#include ("../inc/main_btm.php");?><div id="msg"></div></body></html>

If you run that you'll notice that IE never receives the second response, or it never processes it anyway. I'm not sure why, but I'll look into it. The AJAX object might need to be reset or something like that.Programming, or debugging anyway, can be difficult but it's rewarding when it's finished. It's even more rewarding to see other people using something you made and having it help them do whatever they need to do. So stick with it, I've actually never worked with AJAX myself so this is new to me as well.

Link to comment
Share on other sites

you are very motivitional,do you have any idea what i can do, to accomplish the same task and figure out the IE problem? is thee are ways around. This is not complexGet value form $POSTget result from get_info.php show it in a windowthen send the $POST variable to run_uc.phpand only do it in one loop?i feel like crying. lol

Link to comment
Share on other sites

OK, this version works for me. The issue was what I was thinking, that the AJAX object might need to be reset somehow. I couldn't find out how to do that other then to just re-create it, so I put the code that creates the object into a function that gets called right before the request goes out. So the GetInfo function, for example, will create the AJAX object first, then set the response handler, then send the request. There might be another way to reset the object, but like I said I couldn't find one. I commented out your includes and added a couple debugging statements so you can see the order that things are happening in. Also, when I was researching this I noticed that with a get request you should send null data, but a post request expects there to be some content. So I changed them to get requests and sent null data.

<html><head><?$website = "testing";?><script type="text/javascript">var UsingAs ="<?=$website;?>";var xmlHttp;var t = 0;var timeout;var picked_file;function init_ajax(){  try  {	xmlHttp=new XMLHttpRequest();  }  catch (e)  {	try	{	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	}	catch (e)	{	  try	  {		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	  }	  catch (e)	  {		alert("Your browser does not support AJAX!");	  }	}  }}function resp_openwin(){  document.getElementById("msg").innerHTML += "resp_openwin called<br>";  if(xmlHttp.readyState==4)  {	document.getElementById("msg").innerHTML += "response received, opening window<br>";	picked_file=window.open("http://" + xmlHttp.responseText,"picked_file","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=200");	t++;	timeout = setTimeout("picked_file.close(); GetInfo();",3000);	updateData();  }}function resp_ignore(){  document.getElementById("msg").innerHTML += "response received, ignoring<br>";  return;}function GetInfo(){  if (t <= 100)  {	document.getElementById("msg").innerHTML += "sending request for GetInfo...<br>";	init_ajax();	xmlHttp.onreadystatechange = resp_openwin;	xmlHttp.open("GET","get_info.php",true);	xmlHttp.send(null);  }}function updateData(){  document.getElementById("msg").innerHTML += "sending request for updateData...<br>";  init_ajax();  xmlHttp.onreadystatechange = resp_ignore;  xmlHttp.open("GET", "run_uc.php?usingas=" + UsingAs, true);  xmlHttp.send(null);}</script></head><body><?#include ("../inc/main_hdr.php");?><form name="MyUCIC"><input type="button" onclick="GetInfo();" name="start" value="Start" tabindex="5" /><br><input type="button" onclick="clearTimeout(timeout);" name="stop" value="Stop" tabindex="6" /></form><?#include ("../inc/main_btm.php");?><div id="msg"></div></body></html>

Link to comment
Share on other sites

coolIt is doign what it is supposeto be doing nowthe outcomes issending request for GetInfo...resp_openwin calledresp_openwin calledresp_openwin calledresp_openwin calledresp_openwin calledresponse received, opening windowsending request for updateData...response received, ignoringresponse received, ignoringresponse received, ignoringresponse received, ignoringresponse received, ignoringsending request for GetInfo...and it just keeps going, dont have a clue what that means, but hey, THANK YOUI wish i could tell you that i will not bug yo anymore, but sooner or later, i will feel like dong something, and wala, there i come looking for the master (justsomeguy)

Link to comment
Share on other sites

Those are just messages so you can see the order that things happen in. Each of those messages gets printed by one of the functions. The response functions get called so many times because they get called whenever the status of the request changes, and it looks like the status changes 4 times before it gets the response back. You could add some more messages there to print out what the status is to learn exactly what the process is. I added those so that I could see what was happening in IE.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...