开发者

Live redirect based on periodic server calls with JSON or AJAX

My teacher want to create some virtual lessons for his object and he created a version for the student and one for the teacher.

He now asked me how to redirect students using the teacher control panel, from one part of the lesson to another.

I can do the PHP part for the teacher panel and the server file to be called every 2-3 seconds to see if a redirect is mandatory. Maybe 开发者_高级运维this is a JSON/AJAX thing?

Thanks


You can do in this way. (for student)

from student panel, make a periodic request to server (in interval of 5-10 s).

//data is returned by server as json object
//data.route = true or false as validated from database
//data.location is the location new url

$.post("server.php", { "id": "studentid" },
   function(data){  //data is returned by server as json object
     if(data.route)
     {
        window.location = data.location
     }

   }, 
   "json");

On the server side you can do

if(student is to be routed){ //check from the database
    $data['route'] = true;
    $data['location'] = 'location';
}
else{
    $data['route'] = false;
}
echo json_encode($data);

UPDATE::

put the above ajax code insde a function

function fun1()
{
//put your ajax code herer
} 

and on document ready funcion

$(function(){ setInterval( 'fun1()', 1000) }); 
//use setInterval function
// to call function periodically

you might wanna see this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜