JQuery to PHP via $.Post -- then how to access data??

The internet told me this is supposed to work via (http://stackoverflow.com/questions/2013728/passing-javascript-array-to-php-through-jquery-ajax)

$.post('blocks/basic_test/view.php', { chkdTitlesArray: chkdTitles }

but then how do I access the array chkdTitlesArray in php code on the same file?

This didn't work for me:
<?php $titlesArray = $_REQUEST['chkdTitlesArray'];
       print "print working";
       print $titlesArray;

And I can't find it in the documentation.. (I'm new and this is seeming to be overwhelming to figure out)

View Replies:
JohntheFish replied on at Permalink Reply
You need to read up on tools, actions and ajax in the howtos. There is a lot of background to read before you can make use of any explanation to this.

You may find my AJAX lessons howto and associated addon help (free in the marketplace). They are getting a little dated as they were written for c5.4.1.1, but many of the basic principles remain applicable.

Overall, while returning ajax data to a the view method of a controller can work for a simple demo, that's probably not the way you would want to do it in a real system.
b3rn475 replied on at Permalink Reply
As JohntheFish has said is better to use the C5 proper tools to do that.

But if you like to start using standard "JQuery + PHP" method, just to make it working and then refine all the system, here is a peace of code from one of my projects.


         cache: false,
         type: "POST",
         url: "./ajax/set_chairs_state.php",
         data: {
            jsondata : JSON.stringify(data)
         success: function(data, textStatus, jqXHR){
            if (data == "OK")
               showErrorDialog("Impossible to send data");


$data = json_decode($_REQUEST["jsondata"]);
print_r ($data);

I use the JSON.stringify function that takes a javascript object and transforms it to a JSON string.
Then using the php function json_decode I take the string and transform it to a PHP object.

Here is a reference about mapping between JSON and PHP

jordanlev replied on at Permalink Reply
The problem is you're using 'blocks/basic_test/view.php' as the URL you're POST'ing to -- but you cannot do this in Concrete5, because it doesn't serve up php files at the same location where they exist on your server. Think about it -- you're in the file for a block, but a block doesn't exist at one URL on your site... instead it can be placed on any and every page of your site. So your code needs to say something like "whatever page this block happens to be on now, post to that").

For example, you could do this:
$.post(document.URL, { chkdTitlesArray: chkdTitle });

(document.URL is how you get the current page's URL in javascript... so that's what tells the code "whatever page the user happens to be viewing now... use that").

That being said, I agree with @JohnTheFish that this probably isn't the technique you want to use for a real-world situation. What you should do instead depends on what your real-world situation actually is... so if you could post some details about what it is you're trying to achieve with the code, a more thorough answer could probably be provided by someone. Or go through the learning resources that @JohnTheFish mentioned (http://www.concrete5.org/marketplace/addons/ajax-lessons... ) if you just want to learn more about the system in general.

Best of luck,