3

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)

<?php
     if (isset($_POST["sort"])) 
     {
       $sort = $_POST["sort"];
       echo $sort;
     } 
     else 
     {
       $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. – Derek Hogue Dec 20 '13 at 15:22
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

| improve this answer | |
  • 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. – adamellsworth Dec 20 '13 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 '13 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. – adamellsworth Dec 20 '13 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. – adamellsworth Dec 20 '13 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 '13 at 2:52
0

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">
  <div>
    {!-- A default view value before any date-range AJAX filters are triggered --}
  </div>
</div>

....

<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">
  $(document).ready(function(){

    var ajaxTarget;

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

    trigger.click(function(event) {

      event.preventDefault();

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

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

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

  });
</script>

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">
  <div>
    {if segment_3 == "day"}
        {!-- Populate 'day' data before the template is returned --}
    {if:elseif segment_3 == "week"}
        {!-- Populate 'week' data before template is returned --}
    {if:else}
        {!-- Then it must be 'month' so do populate month data here --}
    {/if}
  </div>
</div>

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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