hi,
I have ajax script which call my own php script. In this script I want to use drupal standard functions. I want to avoid to use including for each php file I use. Is any standard way, how to use drupal functions (as t(), l() etc) in my php file called with ajax?
thanks for advise
Tomas

Comments

suthagar’s picture

tomas.teicher’s picture

thanks for your griff,
I have done everything according the tutorial. But it id not work for me.At least not at all.
I have problem with javascript function imageDetails or with response variable.

var imageDetails = function(response) {
  var result = Drupal.parseJson(response);
  $('.photo-container').html(result.data);
}

after I run ajax, my page is loading neverending. When I give this line to comment

//$('.photo-container').html(result.data);

I see result in my console in firebug, so I think result is ok?

I have very simple data in my function in my module

drupal_json(array('status' =>  0, 'data' => 'hello'));
rkdeveloper’s picture

$('#photo-container').html(result.data);

change .photo-container to #photo-container and let me know if it solves your problem. we usually give div id appended with #before and render html in the div.

tomas.teicher’s picture

I created test div

<div id="test-ajax"></div>

I added it in page.tpl

then I used
$('#test-ajax').html(result.data);
Unfortunately, this does not work, too.

I have this js function:

var imageDetails = function(response) {
  console.log('my test log');
  var result = Drupal.parseJson(response);
  $('#test-ajax').html(result.data);
}

When I look in firebugconsole I see this:
(do not click on the links, it is only sample, how my output looks)

GET http://www.myweb.sk/mypath
my test log
GET http://www.myweb.sk/misc/jquery.js...
GET http://www.myweb.sk/misc/drupal.js...
..and so on. Output of all modules follows and then whole website disappear and blank page appears, but keeps loading neverending.
I do not know where is problem.

When I comment this line

//$('#test-ajax').html(result.data);

I see only this output in firebug console

GET http://www.myweb.sk/mypath
my test log

without loading whole site

tomas.teicher’s picture

I think I know where I made mistake but I cannot fix it. I have this sample code in mymodule.module file

/**
* Implementation of hook_menu().
*/
function mymodule_menu() {
  $items['photos/get/photos'] = array(
    'page callback' => 'mymodule_get_photos_ajax',
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
  );
  return $items;
}
 
function mymodule_get_photos_ajax($nid) {
  drupal_json(array('status' => 0, 'data' => 'data'));
}

but this code is never applied. I can comment whole code and nothing changed on my page. How do I force Drupal site to go through this code? I thought that I should go on page 'photos/get/photos', then I add click() function to some css selector on the site and it is done

Drupal.behaviors.mymodule = function() {
  $('#my_div', context).addClass('mymodule-processed')
  .bind('click', function(){
    $.get('/photos/get/photos, null, imageDetails);
    return false;
  });
}

Should I use another selector than "a" tag?