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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符