Drupal 7 get node id in AJAX

941 views Asked by At

i'm submitting a form to create node using ajax. I can able to create a node using drupal_get_form('node_form', $node) but i need the node id in response. Can anyone help me out to get the node id in ajax response after creating the node.

2

There are 2 answers

0
Fazeela Abu Zohra On

In the node_form function, do something like this,

$node = menu_get_object();
$node_id = $node->nid;
$form_state['#id'] = $node_id;

In the callback function you can get it as,

$id = $form_state['id'];
0
Ilya On

Or you can add the hidden field to the form like this:

$form['hidden-nid'] = array(
    '#type'   => 'hidden',  
    '#value' => menu_get_object()->nid,
);  

and get the value in ajax function:

$id = intval($form_state['input']['hidden-nid']);