php json and xmlhttprequest

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
and77
Forum Newbie
Posts: 8
Joined: Sun Nov 21, 2010 10:03 am

php json and xmlhttprequest

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php json and xmlhttprequest

Post by social_experiment »

“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: php json and xmlhttprequest

Post 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???
and77
Forum Newbie
Posts: 8
Joined: Sun Nov 21, 2010 10:03 am

Re: php json and xmlhttprequest

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: php json and xmlhttprequest

Post 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.
and77
Forum Newbie
Posts: 8
Joined: Sun Nov 21, 2010 10:03 am

Re: php json and xmlhttprequest

Post 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?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: php json and xmlhttprequest

Post 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
and77
Forum Newbie
Posts: 8
Joined: Sun Nov 21, 2010 10:03 am

Re: php json and xmlhttprequest

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