0

In callback of custom menu wrote function , in function I want to use node_add , I have a field that I want add AJAX to it, but when change the field nothing happened where is the problem? I am using Drupal 7

 module_load_include('inc', 'node', 'node.pages');
    $form = node_add('pre_register');
$form['field_course_display_ref']['und']['#value'][]=$nid;
    $form['field_course_display_ref']['#ajax'] = array(
      'callback' => 'myfunction_ajax_change_course',
      'event' => 'change',
      'wrapper' => 'course-schedules-div',
      'method' => 'replace',
      'effect' => 'fade',
    );
    $form['field_course_schedules']['#prefix'] = '<div id="course-schedules-div">';
    $form['field_course_schedules']['#suffix'] = '</div>';

I also try

 module_load_include('inc', 'node', 'node.pages');
    $form = node_add('pre_register');
$form['field_course_display_ref']['und']['#value'][]=$nid;
    $form['field_course_display_ref']['und']['#ajax'] = array(
      'callback' => 'myfunction_ajax_change_course',
      'event' => 'change',
      'wrapper' => 'course-schedules-div',
      'method' => 'replace',
      'effect' => 'fade',
    );
    $form['field_course_schedules']['#prefix'] = '<div id="course-schedules-div">';
    $form['field_course_schedules']['#suffix'] = '</div>';

but nothing happened :(

EDITED I have two field, one field_course_schedules (will wrap) and field_course_display_ref The field that I want but changing it ajax will be trigger.

type mistaked in code corrected but not worked again

  • Reading this should help – Clive Nov 20 '13 at 12:10
  • I read it before wrote this question , but I dont get my answer :( – Yusef Nov 20 '13 at 12:14
  • Did you try something like $form['field_course_display_ref']['und'][0]['value']['#ajax'] or $form['field_course_display_ref']['und'][0]['target_id']['#ajax'] if it's an entity ref field? – Clive Nov 20 '13 at 12:24
  • @Clive I try them right now , but not work :( – Yusef Nov 20 '13 at 12:59
  • are you sure there is a "checkbox-div" on the page? – 2pha Nov 20 '13 at 13:11
0

You're trying to modify a form which is already got built/processed!!

node_add function is internally calling drupal_get_form which will return the processed form, so even if you change anything it wont take any effect. i.e. Drupal will consider the things which are available when the form is being built/processed.

To overcome this you should use drupal_retrive_form instead of drupal_get_form.

The same problem is discussed here with different context, sadly the OP took part in that discussion :(

  • do u sure? my changes taked effrect in value and other attributes , $form['field_user']['und'][0]['target_id']['#value']=$user->name.' ('.$user->uid.')'; $form['field_user']['und'][0]['target_id']['#attributes']=array('disabled'=>'disabled') ; ??? – Yusef Nov 20 '13 at 14:53

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.