douhan4243 2014-05-29 05:52
浏览 38
已采纳

重新加载函数而不重新加载页面

I want to callout a function without the page is reloading. I know that this is possible with AJAX but i don't know how it works with calling a function.

I want to put a timer so it will reload the function every 3 seconds, so the users doesnt need to reload the page everytime to see if there is a new message.

$object = new Messages();
$object->ShowMessage($nick);

ShowMessage(); is the function that i want to call out every 3 seconds.

Full code :

public function ShowMessage() {

    $st = $this->db->prepare("SELECT * FROM bericht");
    $st->execute();
    if($st->rowCount() == 0){
        echo 'There is no message jet!';
    }  

        foreach ($st as $bericht){
        $uid = $bericht['uid'];

        $nick = $this->db->prepare("SELECT * FROM users WHERE id=?");
        $nick->bindParam(1, $uid);
        $nick->execute();

            foreach($nick as $name) {   

            $image = $name['foto'];

        if($image == 'nophoto.jpg'){
        echo '<img src="image/nophoto.jpg" width="60px" height="30px">';

        } else {
        echo '<img src="image/'; echo $image.'"'; echo ' width="60px" height="30px">';
        }
        echo json_encode($name['name']) .': ';
        echo json_encode($bericht['message']).' <br> ';

    }

    }
}
  • 写回答

3条回答 默认 最新

  • doushan1863 2014-05-29 05:59
    关注

    You can do that with ajax. In order to do that, you need to implement ajax client function on frontend and a handler for processing ajax request. In frontend you can use jquery for ajax operations;

    setInterval(function() {
        $.get( "handler.php", function( data ) {
          // You can use data. It is in json format. 
          // Ex: alert(data.message) . "message" is one of the 
          // fields of returned array in php handler file
        }); 
    }, 3000);
    

    handler.php

    <?php
        $object = new Messages();
        $result = $object->ShowMessage($nick); 
        // I assume this returns array. 
        // Ex: array("type" => "error", "message" => "Error occured on feed");
        echo json_encode($result);
    ?>
    

    Update: If you do not want to use json data

    Update your code like below. Only use echo, you do not need to return json data.

    public function ShowMessage() {
    
        $st = $this->db->prepare("SELECT * FROM bericht");
        $st->execute();
        if($st->rowCount() == 0){
            echo 'There is no message jet!';
        }  
    
            foreach ($st as $bericht){
            $uid = $bericht['uid'];
    
            $nick = $this->db->prepare("SELECT * FROM users WHERE id=?");
            $nick->bindParam(1, $uid);
            $nick->execute();
    
                foreach($nick as $name) {   
    
                $image = $name['foto'];
    
            if($image == 'nophoto.jpg'){
            echo '<img src="image/nophoto.jpg" width="60px" height="30px">';
    
            } else {
            echo '<img src="image/'; echo $image.'"'; echo ' width="60px" height="30px">';
            }
            echo $name['name'] .': ';
            echo $bericht['message'].' <br> ';
    
        }
    
        }
    }
    

    and in js;

    setInterval(function() {
        $.get( "handler.php", function( data ) {
          // alert(data);
        }); 
    }, 3000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法