The following allways evaluates if (isset($_POST["sort"])) as false:

// Base template

// Fired on click event (no JS console errors)
$.post( "/path/to/ajax/target/", {sort: day} );

// AJAX target template (PHP enabled)

     if (isset($_POST["sort"])) 
       $sort = $_POST["sort"];
       echo $sort;
       $sort = null;
       echo "no sort data";

I've even tried using POSTMAN to test the AJAX template with post data and it still returns false. I know the ajax works because $.get("/path/to/ajax/target/"); properly returns whatever html i put in it

Any ideas?

  • 1
    What response do you get in the console when you fire the request? Could be a Secure Forms issue. Dec 20, 2013 at 15:22

2 Answers 2


Do you have Process form data in Secure Mode? set to "Yes" in your Security and Session Preferences? That could be your issue.

If you do, then EE will give a "The action you have requested is invalid." error message instead of using your template, and you'll never be able to get your POST data.

I tried your code and it works as long as Process form data in Secure Mode? set to "No".

If you'd like to keep Process form data in Secure Mode? set to "Yes" you can do that, you just need to submit a valid XID along with your other parameters.

<input type="hidden" name="XID" value="{XID_HASH}">

See CSRF Tokens in Templates

  • I tried both routes and neither worked. By default we had ' Process form data in Secure Mode' set to 'No.' While it successfully generated the XID_HASH when set to 'Yes,' it still failed to pass the data. Also, thanks for your response; I think there's only a handful of questions I've had about EE that your posts weren't able to answer. Dec 20, 2013 at 22:02
  • I hope I can help again! When you inspect the response (either from Network tab in dev tools or from a tool like Postman or a similar REST client) what is the content? That will hopefully be very telling.
    – Alex Kendrick
    Dec 20, 2013 at 22:28
  • That's something interesting. It's returning the entire document. However, the data is there. I pass a value of "test" (this time using <?php $something = $this->EE->input->post('sort'); echo $something;?> but both that method and the one above work just fine) and in the response the string 'test' shows up, but on the page the string isn't showing but instead the DOM elements that are in the ajax template surrounding the PHP are there but are empty. This is confusing to write: pastebin.com/MZ04Db3U. In this, I'll be sending the data-orderby value via POST with var selectedRange. Dec 20, 2013 at 23:02
  • Also, because I ran out of room, in the headers the data appears in the FORM DATA as sort:test, which means it's working. Dec 20, 2013 at 23:07
  • Hmmm. I am probably missing the larger picture of what you are trying to accomplish, but it sounds like POST data is being sent and received. So maybe the problem lies elsewhere (with JS or with the intended use of the POST data)?
    – Alex Kendrick
    Dec 21, 2013 at 2:52

Ok so I was being a complete dummy about this. In the end I didn't need to POST any data using the AJAX data: property. All I needed to do was edit what URL I passed :P

HTML and AJAX call on the base template:

<div id="calendar_sort">
    {!-- A default view value before any date-range AJAX filters are triggered --}


<a href="example.com/calendar/ajax/day"  class="trigger" data-orderby="day">Day</a>
<a href="example.com/calendar/ajax/week"  class="trigger" data-orderby="week">Week</a>
<a href="example.com/calendar/ajax/month"  class="trigger" data-orderby="month">Month</a>


<script type="text/javascript">

    var ajaxTarget;

    var trigger = $(".trigger"),
        sort    = $("#calendar_sort > div");

    trigger.click(function(event) {


      var selectedRange = $(this).attr("data-orderby");

      // This replaces the POST data { sort: day } from the question above.
      ajaxTarget = "/calendar/ajax/" + selectedRange + "/";

          type: "GET",
          url: ajaxTarget,
          dataType: "html"
      .done(function(e) {
         $( "#calendar_sort" ).html(e);
      .fail(function(e) {
         console.log("ajax error:" + e);
      return false;


And the /calendar/ajax/<range> template that's being called (defined in my calendar template group simply as ajax as any trailing url data will simply redirect to the ajax template):

<div id="calendar_sort">
    {if segment_3 == "day"}
        {!-- Populate 'day' data before the template is returned --}
    {if:elseif segment_3 == "week"}
        {!-- Populate 'week' data before template is returned --}
        {!-- Then it must be 'month' so do populate month data here --}

We're using Solspace Calendar so generating stuff via AJAX this way is actually pretty nifty and easy once you get the right approach.

Thanks again to @AlexKendrick for your input on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.