From JavaScript into PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terrym
Forum Newbie
Posts: 5
Joined: Wed May 29, 2002 3:51 pm
Location: United Kingdom

From JavaScript into PHP

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
Post Reply