Page 1 of 1

From JavaScript into PHP

Posted: Sat Jun 01, 2002 1:37 am
by terrym
I have this script which does the FIRST part of what I am trying to do.
<script language = "JavaScript">
//Setup variables
var xmlhttp
var XMLHttpRequest
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

//Setup the Request
xmlhttp.open("GET", "homepage.php", true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.responseText)
}
}
xmlhttp.send(null)
</script>
<?
//Do something with the Result

?>
What I want to do now is put the result (responseText) into a variable in PHP so that I can manipulate it further? I expect it's quite simple?

Posted: Sat Jun 01, 2002 2:51 am
by volka
You may set the value of a hidden-text element and submit its form

Code: Select all

...
if (xmlhttp.readyState==4) 
&#123; 
   document.forms&#1111;0].requestVal.value = xmlhttp.responseText;
   document.forms&#1111;0].submit();
&#125;
...&lt;/script&gt;&lt;/head&gt;
&lt;body&gt;
&lt;form method="action"&gt;&lt;input type="hidden" name="requestVal"/&gt;&lt;/form&gt;
....&lt;/body&gt;

Code: Select all

&lt;html&gt;&lt;head&gt;&lt;?php if( !isset($_POST&#1111;'requestVal']) ) { ?&gt;
&lt;script language = "JavaScript"&gt; 
function request()
{
	//var xmlhttp;
	//var XMLHttpRequest;
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") ;
	xmlhttp.open("GET", "homepage.php", true); 
	xmlhttp.onreadystatechange=function()
	{ 
		if (xmlhttp.readyState==4)
		{
			document.forms&#1111;0].requestVal.value = xmlhttp.responseText; 
  		document.forms&#1111;0].submit();
  	}
  };
	xmlhttp.send(null);
}
&lt;/script&gt;&lt;/head&gt;&lt;body&gt;
	&lt;form method="POST"&gt;&lt;input type="hidden" name="requestVal"/&gt;&lt;/form&gt;
	&lt;button onClick="request();"&gt;Get it&lt;/button&gt;
&lt;?php } else
	print('&lt;/head&gt;&lt;body&gt;got: '.$_POST&#1111;'requestVal']);
?&gt;&lt;/body&gt;&lt;/html&gt;