dongtui6347 2010-02-22 03:38
浏览 50
已采纳

如何将变量推送到ajax的web客户端?

Hey guys How do I use the PHP variable $contact['id'] in my ajax call? I'm trying to create a page that retrieves all notifications associated with a user. When the page load I already know the id of the user, how do I use this parameter in my ajax calls from the beginning? At the moment I am using the php preprocessor by splitting up the php string and inserting the variables but I know this isnt a particularly good solution and doesnt for example allow me to refactor the javascript out into its own file. I am using the Zend PHP Framework by the way. Anyone know how best to achieve what I am trying to do?

<?php $this->headScript()->appendScript('
dojo.require("dijit.form.Textarea");

function getNotes(){
    dojo.xhrGet({
        url: "/manager/contacts/get-notes-html",
        content: {
            "contactId" : '.$this->contact['id'].',
        },
        load: function(response) {
                console.log("Form successfully submitted");
                dojo.byId("notesDiv").innerHTML = response;
        },
        error: function() {
                console.error("Error on submission");
                alert("error error errrrror");
        }
    });


}
dojo.addOnLoad(getNotes);
');
?>

<div id="notesDiv">

</div>
  • 写回答

1条回答 默认 最新

  • duanshan188866 2010-02-22 03:42
    关注

    just put your contact_id in a variable in javascript, so you can keep the same code no matter what and it's a bit more clean.

    if you don't want to pollute the global namespace just create your own like data.

    i.e.

    <?php $this->headScript()->appendScript('
     contact_id = ' . $this->contact['id'] .';
    ');
    

    PS: also Zend Framework provide some captureStart() and captureEnd() so you can do that job in the controller

    $this->view->headScript()->captureStart();
    echo "foobar = {$foobar};
    ";
    $this->view->headScript()->captureEnd();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题