【问题标题】:no-jquery ajax update of view after database update using PHP使用 PHP 更新数据库后视图的 no-jquery ajax 更新
【发布时间】:2014-05-26 22:11:51
【问题描述】:

我通过使用 PHP 和 MySQL 构建网站来学习 MVC 和 Ajax。我创建了一个新的控制器方法来处理 ajax,现在可以从我的 Ajax HTTPRequest 成功更新数据库。当我收到响应时,我可以通过用“Hello”之类的 javascript 字符串替换当前分区的内容来更新分区,但我不知道如何用更新的数据库数据替换内容。如何正确更新部门?现在我想在没有 JQuery 的情况下做到这一点,并且不使用 PHP 框架。部门中的数据是成员去过的地方的列表。在 Ajax 之前,列表在页面加载时被发送到“视图”:

//this is controller->index()
public function index() {
    //the properties will be pulled up from the db as neccessary so we need to set them first
    $this->member->setMembername();
    $this->member->setMemberProfile();
    $this->member->setMyPlacesList();
    $this->member->setMemberData(); //puts the above data into one array for convenience
    $this->view->setData($this->member->getmemberData());
    $this->view->render('welcomepage/index'); //builds the view with the data
}

现在在我的 ajax 调用中,我的控制器告诉成员模型更新成员去过的地方列表:

public function AjaxAddVisit() {
    $details['sitename'] = $_POST["sitename"];
    $details['datetime'] = date("Y-m-d H:i:s");
    $details['comments'] = $_POST['comments'];
    $this->member->addMemberVisit($details); This successfully adds the place to the database.  
    //I suppose I should now do something like ?
    //$this->member->setMyPlacesList();
    //$this->member->setMemberData();
    //$this->view->setData($this->member->getmemberData());//this would update the views data
}

我现在想从 Ajax 代码修改视图中的列表,但不知道如何,我是否应该执行以下操作,我只是将 php 代码嵌入到这样的 javascript 字符串中?:

function AddVisitResponse() {
    if (myReq.readyState == 4) {
        if(myReq.status == 200) {
        var phpString = "<?php echo $this->data['myPlacesList'][0][1] ?>";
        document.getElementById('myPlacesList').innerHTML = phpString;
        //this would just print out one element of the list, but just for example
        } else {
        alert("got no response from the server");
        }
    }
} 

上面的输出类似于:myPlacesList'][0][1] ?> 所以我认为一定有问题。似乎我根本无法发回带有 php 代码的字符串,即使我回显“hello”它也不起作用。如何正确更新部门?我应该使用 XML 在我的 php 控制器方法中格式化字符串吗?非常感谢,这是我在这里的第一个问题,任何帮助将不胜感激!!!

【问题讨论】:

  • 您可以在 ajax 调用中返回完整列表(将其放入带有 json_encode 的 json 对象中),或者您可以只返回添加的项目,然后附加它。由你决定。在您的 JS 中嵌入 php 将不起作用,因为 php 只会在页面加载时运行一次
  • @user574632 非常感谢您对使用 JSON 的建议,我听说过它,但不知道它在哪里可能有用。感谢您的建议,我现在已经查找了它,并尝试在我的 responseText 中使用它!谢谢!
  • 没问题,坚持使用 JSON,相比之下 XML 很痛苦。

标签: javascript php ajax templates httprequest


【解决方案1】:

如果没有看到完整的 ajax 代码就很难判断(myReq 变量的范围是什么?),但是从服务器发送回 ajax 脚本的任何内容都应该可以通过 myReq.responseText 访问。

所以你需要将你的函数修改为:

function AddVisitResponse() {
    if (myReq.readyState == 4) {
        if(myReq.status == 200) {
            document.getElementById('myPlacesList').innerHTML = myReq.responseText;
        } else {
            alert("got no response from the server");
        }
    }
}

请注意,您在页面加载时包含在 javascript 中的任何 php,在您运行 javascript 函数时已经被处理。

【讨论】:

  • 非常感谢您的帮助。我不熟悉 myReq.responseText 的工作原理!现在我正在使用 JSON 格式化 responseText。我也可以试试 XML。你为我节省了很多时间!
猜你喜欢
  • 2011-07-10
  • 2016-08-03
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
相关资源
最近更新 更多