Page 1 of 1

php json and xmlhttprequest

Posted: Sat Nov 19, 2011 10:52 am
by and77
Hi all,
i would like to send some information from a php server to a javascript client with json through xmlhttprequest.Is it possible?Can you suggest me some resources?

P.S.I am not interested in any ajax framework.

Re: php json and xmlhttprequest

Posted: Sat Nov 19, 2011 11:02 am
by social_experiment

Re: php json and xmlhttprequest

Posted: Sat Nov 19, 2011 12:42 pm
by califdon
and77 wrote:Hi all,
i would like to send some information from a php server to a javascript client with json through xmlhttprequest.Is it possible?Can you suggest me some resources?

P.S.I am not interested in any ajax framework.
Are you asking about a server "push" mechanism that doesn't start with a request from the client???

Re: php json and xmlhttprequest

Posted: Sun Nov 20, 2011 10:33 am
by and77
I explain what i want to do(it is an execises).I have a server written in php that reads a directory with some photos.I would send this path to the javascript client who modifies the dom with the path that is passed from the php server.Every time i click on the image it is changed.It is a sort of gallery.

Re: php json and xmlhttprequest

Posted: Sun Nov 20, 2011 12:36 pm
by califdon
I'm still not clear. I think you are just describing AJAX, which is the use of the XMLHttpRequest object in Javascript. There is a wealth of information about AJAX online. The critical point is that the process begins with the initiation of the request in the client, where the callback function is also defined, determining what is done with the JSON data when it is received from the server. Then all the server PHP script does is gather the data and format it as JSON and echo it back to the client.

Re: php json and xmlhttprequest

Posted: Tue Nov 22, 2011 1:08 pm
by and77
ok,i have understand.I have done this exercise

html file:

Code: Select all

<html>
<title>Esempio di trasmissione asincrona</title>
<head>
<script type="text/javascript" >
var request=false;
function init_xmlhttprequest(){
try{
	request = new XMLHttpRequest();
	}catch(failed){
			request=false;
			}
	if( !request){
	         return false;
	}
	else return request;
	
}
function broadcast(){
 request= init_xmlhttprequest();
if( !request ){
		alert("error initializing request");
		return false;
		}
var url="prova_ajax/php/manage_response.php";
request.open("GET",url,true);
request.onreadystatechange=manage_response;
request.send(null);
}
function manage_response(){
	if( request.readyState == 4){
			if( request.status == 200){
					document.getElementById("risp").innerHtml=request.responseText.replace(/\n/g,"<br>");
					}
					else{
							alert("Response status "+request.status);
					}
			}
}
</script>
</head>
<body>
<h1>Prova Ajax</h1>
<input type="button" name="button" value="Trasmetti" onclick="broadcast()">
<div id="risp">
</div>
</body>
</html>
php file:

Code: Select all

<?php
$mess="Richiesta ricevuta";
echo($mess);
?>
My doubt is the php program.Is it right?

Re: php json and xmlhttprequest

Posted: Tue Nov 22, 2011 2:13 pm
by califdon
The php script is OK for plain text. You mentioned JSON earlier, which is a different way to format data. If you need JSON, then your php script must format the data before echoing it.

Your Javascript has several problems. You need to define the callback function (what the browser is going to do with the data it receives) in the "onreadystatechange" method of the object you create with XMLHttpRequest. So the callback function is executed when there is a change in the status of the request and the new status = 4, meaning that the data has been received.

Here are a few tutorials with code examples that illustrate how to code AJAX:
http://code.google.com/edu/ajax/tutoria ... orial.html
http://www.w3schools.com/ajax/ajax_intro.asp (after reading each page, click 'Next Chapter')
http://www.tizag.com/ajaxTutorial/ajaxform.php

Re: php json and xmlhttprequest

Posted: Wed Nov 23, 2011 10:26 am
by and77
Ok,is all right now.I have understand.Thank you.

P.s.I have found this articles too http://www.ibm.com/developerworks/web/l ... index.html this is the first of eleven articles very usefull.