4

I'm simply trying to submit a node add form via AJAX:

function input_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'page_node_form':
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#ajax' => array(
            'wrapper' => 'apple-node-form',
            'callback' => 'node_add_callback',
            'method' => 'replace',
            'effect' => 'fade'
        )
      );
      break;
  }
  return;
}

function node_add_callback(){
  $node = new stdClass();
  $node->type = 'page';
  return drupal_get_form('node_form', $node);
}

input_form_alter() DOES get called. And when I click the submit button, I even see the standard ajax gif and "Please wait" message. The gif and wait message then disappear, as expected. However, no new node has actually been created.

  • So the person is on the node add screen, and when they hit submit you want it to submit the form using AJAX? You might want to add a drupal_set_message("node_add_callback was called"); to the top of your node_add_callback to make sure it's getting called. My head is getting wobbly trying to understand the goal of this one... – Johnathan Elmore May 1 '12 at 18:44
  • Yes, node_add_callback is getting called. Not sure why creating content via AJAX is such a strange goal. It's about to happen when I click the add comment button to the right... – maxedison May 1 '12 at 20:45
2

Solved it. The problem was this code:

  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#ajax' => array(
        'wrapper' => 'apple-node-form',
        'callback' => 'node_add_callback',
        'method' => 'replace',
        'effect' => 'fade'
    )
  );

This overwrites the $form['actions']['submit'] array that already existed, recreating it with the values I specified. Since all I wanted to do was add an AJAX submission, the following code should replace the above:

    $form['actions']['submit']['#ajax'] = array(
        'wrapper' => 'apple-node-form',
        'callback' => 'node_add_callback',
        'method' => 'replace',
        'effect' => 'fade'
    );

In other words, we're just adding an #ajax item to the already created $form['actions']['submit'] array.

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.