从 Jquery/Ajax MODx 调用片段
我有一个 select 元素,它在更改事件上使用 jQuery 进行发布,我试图将其发布到片段并获取结果,但是似乎如果直接从 javascript 调用片段,则不会有($modx
) 对象,我无法使用 PDO 访问数据库,我的代码如下:
$(document).ready(function() {
$('#camplist').change(function() {
$.post('core/components/evoprograms/snippets/register-camp.php?action=getCamp&id=' + $(this).val(), function(data) {
$("camp-details").show();
$('.result').html(data);
});
});
});
正确的方法是什么?
I have a select element that does a post using jQuery on a change event, I was trying to post it to a snippet and get the results back, however it seems that if the snippet is called directly from javascript there will be no notion of the ($modx
) object and i can't access the DB using PDO, my code is as follows:
$(document).ready(function() {
$('#camplist').change(function() {
$.post('core/components/evoprograms/snippets/register-camp.php?action=getCamp&id=' + $(this).val(), function(data) {
$("camp-details").show();
$('.result').html(data);
});
});
});
What's the right approach to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不好 - 你不希望任何人访问 /core/* 下的任何内容
基本上有两种方法:
最简单的方法 - 创建一个资源(没有任何模板),其中只有您的代码片段调用作为内容(未缓存!),然后您 $.post 到该资源。
革命方式(更干净,更好) - 使用您自己的连接器(/assets/components/evoprograms/connector.php)。 ://rtfm.modx.com/display/revolution20/PHP+Coding+in+MODx+Revolution,+Pt.+III" rel="nofollow">查看此了解更多信息。或者直接复制任何内容现有的连接器并根据需要进行修改。
This is not good - you don't want anyone accessing anything under /core/*
Basically there are two ways:
The simplest way - create a resource (without any template) with only your snippet call in it as content (uncached!). then do you $.post to that resource.
the revolution way (cleaner, better) - use your own connector (/assets/components/evoprograms/connector.php). see this for more info. or just copy any existing connector and modify if needed.
您可以以其他方式执行此操作,例如 - 您可以在
onchange
事件上提交表单,并为表单提供action
来调用所需的代码片段...例如,请参阅下面的代码:-
这不需要任何 JavaScript。
You can do it other way like - you can submit the form on an
onchange
event and give the formaction
to call that snippet required...For example see the below code:-
And this doesn't need any javascript.
在 core/components/evoprograms/snippets/register-camp.php 脚本中加载 modx 对象,
Load the modx object in your core/components/evoprograms/snippets/register-camp.php script,