Package Ajax

Permalink
Hello everybody,

I made a package which add dashboard single page,

In this single page, i want to make a simple ajax request but ajax not found url on my ajax php file.

For my example :

$.ajax({
   url : "<?php echo $this->url($resultTargetURL) ?>/index.php/packages/configurateur_devis/sauvegarde.php",
   type : 'POST',
   data: 'idQuestion='+idQuestion+'&nom='+$(lien).parent().find('input.question').val()+'&prix='+$(lien).parent().find('input.prix').val()
}).success(function(msg)
{
   alert(msg);
});


Ajax request success but i think sauvegarde.php is not found.

Can you help me please ?

Thank you in advance ;-)

Emixam25
 
hutman replied on at Permalink Reply
hutman
You can use this helper to get the full URL to your tools file in Concrete 5.6

$url = Loader::helper('concrete/urls');


Replace your line

"<?php echo $this->url($resultTargetURL)?/index.php/packages/configurateur_devis/sauvegarde.php"


With

"<?phph echo $url->getToolsURL('sauvegarde.php', 'configurateur_devis'); ?>"
JohntheFish replied on at Permalink Reply
JohntheFish
Have a look at my AJAX lessons howto and associated addon. It includes working examples of the code @hutman suggests.
Emixam25 replied on at Permalink Reply
Emixam25
Hello, thank you for your answers,

But it not works in 5.7 :(
rge replied on at Permalink Reply
This is an article about Ajax in 5.7. Tools are deprecated and you can now use methods / views in your controller.

http://c5hub.com/learning/ajax-57-style/...
Emixam25 replied on at Permalink Reply
Emixam25
Hello rge and thank you,

I have already found this link but i not understand everything.

In my package, how to modify application/bootsrap/app.php ?



If i have a controller for a single page in my package, can i create ajax method and call this in javascript simply ?
rge replied on at Permalink Best Answer Reply
You don't need to.

In the view file. You make the Ajax call like this

$(document).ready(function(){
   $.ajax({
      url : "<?php echo URL::to('/products', 'ajax_call'); ?>" ,
      success : function(rs){
         $('#ajax-content').html(rs);
      }
   });
});
// "/products" is the path of the single page
// "ajax_call" is the function that will be called in the controller of the single page.


products.php (single page)
Controller ajax_call function
public function ajax_call()
   {
      echo "String";
      exit;
   }


Also read this part of the documentation
http://www.concrete5.org/documentation/developers/5.7/working-with-...
Emixam25 replied on at Permalink Reply
Emixam25
Thank you ! It works perfectly :)

One last question, what is the use of 'exit' ?
rge replied on at Permalink Reply
If you don't exit the full html will be returned. In this example it will be only the string.
Emixam25 replied on at Permalink Reply
Emixam25
Ok thank you, have a nice day !
Emixam25 replied on at Permalink Reply
Emixam25
Hello all, i have another question about ajax, for single pages, ajax in controller works,

but in my block, how i call ajax method in block controller ?


I try to put my ajax method in controller already installed with package but redirect to login page.

Anybody have a solution ?

Thank you :)
rge replied on at Permalink Reply
You should read this article to get the needed information:http://www.concrete5.org/documentation/developers/5.7/working-with-...


Step 1
In the view.php you need to make an Ajax call. Use
$view->action('');
to create the url.

The code should look like this.
$(document).ready(function(){
  $.ajax({
   url : '<?php echo $view->action("ajax_call") . "/" . $bID ?>',
   success : function(rs){
      $('#ajax-content').html(rs);
   }
  });
});

In the documentation is mentioned that $view->action(); provides automatically the bID. This seems to be not the case. That is why I append it my self.

Step2
Create the action in the controller. This should have the same name as provided in the action in the view prefixed with "action_".

public function action_ajax_call($bID  = false){
   //check if the ajax call was done by this block
   if( $this->bID != $bID){
      exit;
   }
   echo "string";
   exit;
}
Emixam25 replied on at Permalink Reply
Emixam25
Work like a charm, Thank you rge !