2

I have a text field and a ajaxified link in my form:

$form['id_number'] = array(
    '#title' => t('ID Number'),
    '#type' => 'textfield',  //textfield  
    '#suffix' => '<div>l(t('101-03043'), 'myid_choose_result/101-03043/1, array('attributes' => array('class' => 'use-ajax')))</div>',  //ajaxified link 
    '#attributes' => array('id' => 'myid_search_id_number'),
); 

The link above calls a function myid_choose_result define in my hook menu:

function myid_menu(){
    $items = array();

    $items['myid_choose_result'] = array(      
        'page callback' => 'myid_choose_result',
        'access callback' => 'myid_user_access',
        'file' => 'myid.pages.inc',
        'type' => MENU_CALLBACK,
    );
    return $items;
}

What I want to achieve is every time the user clicks the link, the text field value will be the link's text. After doing so, I want to call a another function:

function myid_choose_result($id,$template){
      //Change the textfield's value into $id
      myid_some_function(); //Call another function
}

How will I do that?

  • when click on link what happened? – David Mcsmith Aug 5 '15 at 6:55
  • @DavidMcsmith When the link is click, the function myid_choose_result is called. – user46175 Aug 5 '15 at 8:43
  • @Aliyah did you try my solution? – Yuseferi Aug 5 '15 at 15:50
0

I suggest you instead of creating link and try to call ajax with old method( call form your custom js file ) try drupal ajax method

My suggestion is :

function yourmodule_form_alter(&$form,$form_state,$form_id){
if($form_id=='your specified form name '){
$form['id_number'] = array(
    '#title' => t('ID Number'),
    '#type' => 'textfield',  //textfield  
     '#prefix' => '<div id="vm-container">',    
    '#suffix' => '</div>'
    '#attributes' => array('id' => 'myid_search_id_number'),
 );
$form['myajax'] = array(
        '#type' =>'button',
        '#value' => t('fill me'),
        '#ajax' => array(
            'callback' => '_myfuction_fill_number_ajax',
            'wrapper' => 'vm-container',
            'method' => 'replace',
            'effect' => 'fade',
            'prevent'=> 'sumbmit',
        ),
    );
 }
}
function _myfuction_fill_number_ajax(&$form,$form_state){
     $form['id_number'] ['#default_value'] = myid_some_function(); 
    // if not work try it with  $form['id_number'] ['#value'] = myid_some_function(); 
      return $form;
    }

also if you can want do ajax with jquery, add js file to your form, in that js file set click handler for your link ( tag) and in click call $.ajax with you menu rout.

0

You need to format the response of your callback correctly.

First of all define the menucallback as following:

/**
 * Implements hook_menu().
 */
function my_module_menu() {
  $items = [];

  $items['my_module/test/%/%'] = array(
    'page callback' => 'my_module_test_callback',
    'page arguments' => [2, 3],
    'access callback' => TRUE,
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

Then your callback function need to return an array containing info about ajax commands

function my_module_test_callback($var_0 = '', $var_1 = '') {

  // Build your text value here according to var_0 and 1.
  $text_val = $var_0 . ' ' . $var_1;

  $commands = [];
  // This will add a command that in JS side will translate in something like this: $('#myid_search_id_number').val('new_value');
  // The textfield value will be updated.
  $commands[] = ajax_command_invoke('#myid_search_id_number', 'val', [$text_val]);

  // return an array that "ajax_prepare_response" can read and prepare for js side
  return ['#type' => 'ajax', '#commands' => $commands];
}

Your Answer

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