开发者

Call Snippet From Jquery/Ajax MODx

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?


$.post('core/components/evoprograms/snippets/register-camp.php

This is not good - you don't want anyone accessing anything under /core/*

Basically there are two ways:

  1. 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.

  2. 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.


You can do it other way like - you can submit the form on an onchange event and give the form action to call that snippet required...

For example see the below code:-

<form action="[[!snippetname]]" method="POST">
  <h3>dropdown: 
    <select name="selection" onchange="this.form.submit();">
      <option>select</option>
      <option value="2">all</option>
     </select>
   </h3>
 </form>

And this doesn't need any javascript.


Load the modx object in your core/components/evoprograms/snippets/register-camp.php script,

define('MODX_API_MODE', true); 
// Full path to the index
require_once('/path/to/modx/public_html/index.php');
$modx = new modX();
$modx->initialize('mgr');
//your post
$your post here = $_POST['register'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜