We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38926
    • 33 Posts
    Hello there.

    I'm fidling around with adding an AJAX based search form for a product database, but I'm stuck already in the beginning steps. Basically I'm having problems with sending parameters with my AJAX request. The problem is either so simple that I dont see, or so intricate to Modx that I don't know it.

    This is the bare bones of what is going on.

    I got my page where the results are to be displayed. It also contains this bit of jquery:
    window.onload = function(){
    
    $.ajax({
      url: "index.php?id=25&name=John",
      cache: false
    }).done(function( html ) {
      $("#searchResults").append(html);
    });
    
    }
    


    The "id=25" is the ID of the resource handling AJAX requests and the "name=John" is my hardcoded parameter for test purposes.

    The resource with ID of 25 simply contains this snippet:
    [[!ajax]]

    There's no template, no nothing for it. It's been published but is not "visible".

    Now in the snippet, if I write this:
    $output = "John";
    
    return $output;


    It returns properly and is displayed like it should on my page. But if I write this:
    $output = $_GET["name"];
    
    return $output;


    Nothing comes out.

    Firebug tells me that the GET does make it to the page, but if I do a
    print_r($_GET);

    It returns a list of GET's that doesnt contain my get.

    Is there some hidden trick I should be aware of as to how to get the GET's and POST's?
      • 38926
      • 33 Posts
      Oh well had a hunch and that fixed it.

      It seems that when targeting the ajax resource as index.php?id=25, it does in fact not target the actual ajax resource, but instead performs a redirect where my GET request is lost. At least that's my theory as to what happens.

      I've fixed it by making the site URL friendly and can now target the ajax resource directly with it's alias, so now it uses this url: "ajax.html?name=John" and properly returns an echo of the name that I sent it.