0

I am developing a custom payment gateway for Drupal Commerce. I am getting some problems calling a custom module function from an AJAX URL.

How do I call a module function from a custom AJAX request?

function custom_gateway_settings_form($settings) {
  $form = array();   

  $form['code'] = array(
    '#type' => 'textfield',
    '#title' => t('Code'),
    '#maxlength' => 100,
    '#description' => t('Code.'),  
    '#prefix' => '<div id="tabs-code" style = "display:none">',
    '#suffix' =>'</div>',
  );

  $form['btncode'] = array(
    '#type' => 'submit',
    '#id' => 'btncode',
    '#value' => 'Get the code',
  );
}

The JavaScript code is the following one.

   jQuery("#btncode").click(function() { 
     jQuery.ajax({
       type: "POST", 
       url: Drupal.settings.basePath + "/?p=ajaxGetCode",
       cache: false,
       beforeSend: function() {
         // jQuery('.loadrimage').show();
         // jQuery(".error").hide();
       },
       complete: function() {
         // jQuery('.loadrimage').hide();
       },   
       success: function(htmlText) {
         alert(htmlText);
       }
   });
1

You have to create own endpoint and call post on it. For example create menu element using hook_menu:

$items['my-module-ajax-endpoint'] = array(
   'page callback' => 'my_module_ajax_callback', 
   'delivery callback' => 'ajax_deliver', 
   'access arguments' => array('access content'), 
   'type' => MENU_CALLBACK,
);

And now post to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.