I am using hook_menu to setup a link which I'm calling via JQUERY from an AJAX application.
My hook implementation is as follows:
function staff_filter_menu(){
$items = array();
$items['staff/filtering/results/%'] = array(
'page callback' => 'staff_filter_function',
'page arguments' => array(3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'delivery callback' => 'staff_filter_deliver',
);
return $items;
}
This link is the point of contact for my ajax app which is a simple textfield where users type in some input. *staff_filter_function* searches the database and returns some matching data. Ajax code is as follows:
$.ajax({
url: Drupal.settings.CALL_BACK + '/' + $(this).val(),
success: function($data){
.... more stuff done here
}
});
It all works perfectly well but for one small detail.
I want to allow a shortcut which will allow all results to display. This shortcut is simply typing a single space.
But, because hook_menu is expecting an argument at the end of the url, my ajax request fails whenever I type in a space. It works perfectly when I type in real content.
This is just for completeness and it a nice to have feature ... if anyone can share some ideas, I'll be very great greatful.
Maybe you don't need to add an argument at the end of the URL.
Just make change you code to the following:
function staff_filter_menu(){
$items = array();
$items['staff/filtering/results'] = array(
'page callback' => 'staff_filter_function',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'delivery callback' => 'staff_filter_deliver',
);
return $items;
and make a slight change to your page callback function
function staff_filter_function($my_arg = "")
{
// your code goes here
}
and you can treat $my_arg as your page argument.
Hope it works,
Muhammad
Related
I have tried to acces it through an ajax call and by simply going to it by url,
It always give me a 404
i have flushed my caches multiple times and even tried to remove and re-add the module (as i have had the problem with other modules and read on other responses on the problem)
i also have looked it up on internet, but i can't seem to find any solution
(module name : TTK_rest)
.module:
function TTK_rest_menu() {
$items = array();
$items['TTK_rest_api/TTK_task_progression'] = array(
'page callback' => 'TTK_task_progression_view',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function TTK_task_progression_view(){
return '{"pom":"pom"}';
}
and the ajax call to it:
$.ajax({
url:'/TTK_rest_api/TTK_task_progression',
data: {"getProgress": "true"},//, "event_id":settings['TTK_task_progression']['jsEvent_id']
type: "POST",
contentType: "JSON",
success: function(data){
var $data = $(data);
console.log(data);
},
error: function(err){
console.log("neupe, try again");
}
});
finally found the solution (posted the question after multiple hours of searching, if i've known i would have find an answer this quick i wouldn't have asked it.. still going to leave it up with a response in case there is someone with the same problem
solution to the problem was that i had to have my function name had to have the module name prefix
note: i have an other module called 'TTK_task_progression', might be the origin of the problem i had
solution:
function TTK_rest_menu() {
$items = array();
$items['TTK_rest_api/TTK_task_progression'] = array(
'page callback' => 'TTK_rest_progression', // <- changed
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function TTK_rest_progression(){ // <- changed
return '{"pom":"pom"}';
}
Im in drupal 7, i need to make a select with many options, depends of the option taken, in a textarea will be loaded several values in a string.
After hours of test i come here for help.
Im working on a basic page:
function ajax_load_all_admins($form, &$form_state) {
$form = array();
$form['changethis'] = array(
'#type' => 'select',
'#options' => array(
'' => '',
'1' => 'Cargar todos los admins'
),
'#ajax' => array(
'event' => 'change',
'callback' => 'ajax_load_all_admins_callback',
'wrapper' => 'listaCorreos-div'
)
);
$form['listaCorreos'] = array(
'#type' => 'textarea',
'#prefix' => '<div id="listaCorreos-div">',
'#suffix' => '</div>'
);
if (!empty($form_state['values']['changethis'])) {
$payments_list = db_query('QUERY WORKING WELL');
$value = '';
foreach ($payments_list as $payment) {
$value .= $payment->admin . ',';
}
trim($value, ',');
$form['listaCorreos']['#default_value'] = $value;
}
return $form;
}
function ajax_load_all_admins_callback($form, $form_state) {
return $form['listaCorreos'];
}
$form = drupal_get_form('ajax_load_all_admins');
print drupal_render($form);
The Ajax call is working but i only recibe:
0: {command:settings, settings:{basePath:/, pathPrefix:,…}, merge:true}
No other one position.
I think it can be for the drupal_render, but dont know why?
Thanks in advice.
since the ajax itself works;
Looks to me like the db_query isn't working well and/or returning unexpected results.
My advice: You should be able to debug. i.e. setting a breakpoint and stepping into your code line by line
I do it with netbeans & XDEBUG
This should gives you a great edge solving this & upcoming similar problems, as you'll be able to monitor your variables & the execution tree of your code
Best of luck.
--edit-- This should be totally in the comment section, but ... new here , cant comment at the moment.. apologies.
I was doing this ajax functions in a simple view, created to make an specific form.
I move all the logic to a new module, instead of simple view and now is working.
I take a look to the ajax examples of "examples module" and test if they works on a simple view,like my code, dont works.
I think for any reason, drupal ajax only works if the render is not manually, like i was doing.
Thanks.
I had created several modules extending the UI elements of the FormAPI. This works fine when I render forms in a normal way (not AJAX). But if the rendered form is delivered though the AJAX request, JS script simply inserts the code via the $.html function. So I can't access elements of the document by their ID from the script evaluated by the $.html.
Is there any solution to pass JavaScript code through the Form/Ajax API?
I think jQuery("#new-id") (not $(..)) should take the newest dom elements.
You can use either the 'ajax' property of form api
$form['myitem'] = array(
'#type'=>'textfield',
'#ajax' => array(
'callback' => 'my_callback',
'wrapper' => 'new-id',
'method' => 'html',
'effect' => 'fade',
),
);
$form['myitem2'] = array(
'#type'=>'markup',
'#value'=>"<div id='new-id'> I'm #new-id </div>"
);
function my_callback(&$form, &$form_state){
$commands = array();
$commands[] = ajax_command_invoke('#new-id', 'html', array('<div id="new-id2">#new.id2 here!</div>'));
$commands[] = ajax_command_invoke('#new-id2', 'addClass', array('error'));
return array('#type' => 'ajax', '#commands' => $commands);
}
D7 ajax_commands:
http://api.drupal.org/api/drupal/includes--ajax.inc/group/ajax_commands/7
hope this help
I'd like to call a form that is created in the .module file. It means that in a AJAX function located in a HTML page of drupal, I'd like to append a form that is defined in the module file with a DIV presents in my HTML file.
Do you have a solution to have only the code HTML generated by drupal for a form? But the call to Drupal would be in an AJAX Function.
Actually, I can create a path '?myProject/form' where the form is generated. However, I cannot append the full content of the html page (header, footer of drupal). I only want to append the HTML code of the form.
I hope that I'm understandable
Thank you very much
Bat
Maybe you have a 'page callback' with a function like this in your hook_menu:
$items['myProject/form/%'] = array(
'title' => t('Get a form'),
'description' => 'Get form',
'access callback' => 'user_access',
'page callback' => '_get_form',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
And maybe you have this function defined:
function _get_form($form_id){
return "Hello World with " . $form_id; //returns the whole page with this text
}
If you change return for print, Drupal only return this text, like this:
function _get_form($form_id){
print "Hello World with " . $form_id; //returns only this text
}
I hope it helps
I'm trying to get an AJAX-submitted (AHAH) form working to display in a block on the sidebar. For testing purposes, I'm using an example module called "Poof" from the Pro Drupal Development book: http://books.google.com/books?id=VmZrdGuBZCMC&lpg=PA269&ots=cnHiYG6kXn&dq=pro%20drupal%20development%20poof&pg=PA269#v=onepage&q=pro%20drupal%20development%20poof&f=false
The only thing I've added to the example so far is an implementation of hook_block, which looks like this:
function poof_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('poof');
return $blocks;
case 'view':
$block['content'] = drupal_get_form('poof_form');
return $block;
}
}
The AJAX module works fine when displaying on its own page (mydrupalsite.com/poof) but when I call the form with module_invoke('poof', 'block' ...) in a template file, the form submits as normal (sans AJAX) and refreshes the page.
I can't find a definitive answer for why this happens, though a found something tangentially related that suggests that maybe AHAH doesn't work within blocks. If that's so, why? Or better yet, what's a work-around. Do I have to put the on its own blank page and bring it in with an iframe? That sounds unnecessarily messy.
UPDATED:
Here's more code for reference (again, it's from the Pro Drupal book)
function poof_form() {
$form['target'] = array(
'#type' => 'markup',
'#prefix' => '<div id="target">',
'#value' => t('Click the button below.'),
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'button',
'#value' => t('Click Me'),
'#submit'=>false,
'#ahah' => array(
'event' => 'click',
'path' => 'poof/message_js',
'wrapper' => 'target',
'effect' => 'fade',
),
);
return $form;
}
function poof_message_js() {
$output = t('POOF!');
drupal_json(array('status' => TRUE, 'data' => $output));
}
Try adding
$blocks[0]['cache'] = BLOCK_NO_CACHE;
to your hook_block implementation.
Rendering a form with ahah causes a call to drupal_add_js to add the ahah javascript, but while the output of the block is cached, the javascript that gets added to the page doesn't.