Ajax call and action with parameter.

Permalink
Hi Guys,

I'm a newbie of ajax and concrete5 adn I'm hoping that someone knows the answer on my problem. I followed Andrew Embler's tutorial about ajax and it's working fine. The problem is I wanted my function to have one parameter like below.

on controller.
public function action_test($a) {
  echo '<h3>The values is: '.$a.'</h3>';
  exit;
}


on edit.php
$action = $this->action('test');
<a id="test_action" href="<?php echo $action; ?>">Ajax Call;</a>


on auto.js
$('#test_action').click(function() {        
      var url = $(this).attr('href');
      $.get(url, function(r) {
        console.log(url);
             console.log(r);      
         alert('Success!');
      });
      return false;
   });


I wonder how can I pass an argument on my action test using the method that I presented above. Thank you.

 
JohntheFish replied on at Permalink Reply
JohntheFish
Unless you are on a single page, you will need to pass the parameter as a $_PUT or $_GET parameter and retrieve them from the php $_PUT or $_GET (or the c5 object versions of these).

Also, beware of doing this in an edit dialog. The ajax could fail during the initial add of the block becuae the bID does not yet exist.

See AJAX Lessons
http://www.concrete5.org/documentation/how-tos/developers/ajax-less...
and the associated addon
http://www.concrete5.org/marketplace/addons/ajax-lessons/...
Its getting a bit old now, but the principles remain the same.
dedano replied on at Permalink Reply
@JohntheFish, you're right, it fails on add. How do I do this on Add/Edit form of the block?

P.S. I was able to append a Get Param on the url, it works as usual as GET method. I thought there's a special way of doing it. :)
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
You need to call out to a tool file, which is one of the examples in AJAX lessons.
dedano replied on at Permalink Reply
Thank you for your help so far, it is working. Iw as able to create a tool file and url was constructed. Unfortunately using the code I posted above on my auto.js. It's not returning anything on the response.

But the url is correct because when I removed return false; it's being directed on the the tool file.

THis behavior is the same on the Ajax lesson. YOu mentioned that I needed to create a ccm_token myself? May you please give me an idea on how to do that. Thank you.
JohntheFish replied on at Permalink Reply
JohntheFish
What you need to look up is:
Loader::helper('validation/form')


You use this in a block to create an encrypted token, then again in a tool to check the validity of that token.

PS:
http://www.concrete5.org/documentation/developers/forms/basic-valid...