【问题标题】:Reload a function without reloading the page重新加载函数而不重新加载页面
【发布时间】:2014-05-29 05:52:49
【问题描述】:

我想在不重新加载页面的情况下调用一个函数。 我知道这对于 AJAX 是可能的,但我不知道它是如何与调用函数一起工作的。

我想设置一个计时器,让它每3秒重新加载一次功能,这样用户就不需要每次都重新加载页面来查看是否有新消息。

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

显示消息();是我想每 3 秒调用一次的函数。

完整代码:

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> ';

    }

    }
}

【问题讨论】:

  • 嗯,我不知道它是否会工作,但你可以通过 jquery 重新加载一个 div,然后在那个 div 里面你可以放你的 php 函数。
  • 您可以使用 ajax 和一个简单的处理程序来做到这一点。更多详情见我的回答here

标签: php ajax oop


【解决方案1】:

你可以用 ajax 做到这一点。为此,您需要在前端实现 ajax 客户端功能和用于处理 ajax 请求的处理程序。在前端,您可以使用jquery 进行ajax 操作;

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);
?>

更新:如果你不想使用 json 数据

如下所示更新您的代码。只使用echo,不需要返回json数据。

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> ';

    }

    }
}

在js中;

setInterval(function() {
    $.get( "handler.php", function( data ) {
      // alert(data);
    }); 
}, 3000);

【讨论】:

  • 这个回显 "NewOne": "New Message" 但在我 $.get 时没有显示 handler.php
  • @user3674846 你能把你的代码放在你的问题中吗?
  • @user3674846 您在代码中返回了很多东西。首先,决定要返回什么数据(您可以将数据作为 key=>value 放入数组中),或者您可以将所有内容作为普通 html 回显。能否请您构建一个数组并用您的数据填充它,然后只放一个echo json_encode($yourArr);
  • 我需要所有数据来返回图像、昵称和消息,我不知道如何填充它,我是新手。 alert(data.message) 给出未定义的testoop.hopto.org
  • @user3674846 你能看看我更新的答案吗?更新部分
【解决方案2】:

您需要将 javascript 集成到您的 php 代码中。 使用jquery,可以看一下$.post函数。您可以使用 javascript 函数 window.setInterval(yourFunction(),3000)

发起活动

您还需要创建一个将回复 Ajax 请求的 php 页面,该页面将作为参数传递给 $.post

【讨论】:

    【解决方案3】:

    只需通过 ajax 调用该脚本:

    .html:
    setInterval(function(){
      $.ajax({
        url: "showMessageScript.php",
        type: "post",
        data: {nick: $("#nick").val(), token: <?php echo $_SESSION['token']; ?>},
        success: function(jsonData){
            $('#display').text(jsonData['message']);
        }
      });
    },
    3000);
    

    showMessageScript.php:
    
    function showMessage($nick) {
       $object = new Messages();
       $object->ShowMessage($nick);
       echo json_encode(array('message': 'Your message is here'));
    }
    
    // check if authorized to do some action
    if (!empty($_SESSION['token']) && $_SESSION['token'] == $_POST['token'] && !empty($_POST['nick'])) {
        showMessage($nick);
    } else {
        echo json_encode(array('message': 'not authorized'));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-21
      • 2012-06-15
      • 2013-09-18
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      相关资源
      最近更新 更多