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.
php json and xmlhttprequest
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: php json and xmlhttprequest
Have a look at this http://dereklawless.ie/articles/an-intr ... n-to-ajax/
“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
Re: php json and xmlhttprequest
Are you asking about a server "push" mechanism that doesn't start with a request from the client???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.
Re: php json and xmlhttprequest
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
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
ok,i have understand.I have done this exercise
html file:
php file:
My doubt is the php program.Is it right?
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>Code: Select all
<?php
$mess="Richiesta ricevuta";
echo($mess);
?>
Re: php json and xmlhttprequest
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
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
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.
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.