如何将变量推送到web客户端以获取ajax?
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?
嘿伙计们如何在我的ajax调用中使用PHP变量$ contact ['id']?我正在尝试创建一个页面来检索与用户关联的所有通知。当页面加载我已经知道用户的id时,如何从头开始在我的ajax调用中使用这个参数?目前我通过拆分php字符串并插入变量来使用php预处理器,但我知道这不是一个特别好的解决方案,并且例如允许我将javascript重构为自己的文件。我顺便使用Zend PHP Framework。任何人都知道如何最好地实现我想做的事情?
<?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 个解决方案
#1
1
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.
只需将你的contact_id放在javascript中的一个变量中,这样你就可以保持相同的代码,无论它是什么,它都更干净。
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
PS:Zend Framework还提供了一些captureStart()和captureEnd(),因此您可以在控制器中完成这项工作
$this->view->headScript()->captureStart();
echo "foobar = {$foobar};\n";
$this->view->headScript()->captureEnd();
更多相关文章
- 传递变量以便以角度移动子弹
- js学习之路2: JavaScript 变量
- 让Heroku的配置变量在node.js中工作
- 深究js(三)——变量
- javascript变量:全局?还是局部?这个得注意
- 在Javascript / node.js中共享模块之间的变量?
- 【问题解决方案】ImportError: No module named 'pygal'
- Python 黏包及黏包解决方案
- 求助:Python是否可以用一行代码来同时给变量赋值并打印变量的值