Update a field based on value of another field in node edit form In the node add/edit form, I have a node reference field. When users select a node, I want to update another field with value of a field of selected node.

What's the best and easiest way to do this?

I want to load the value in the edit form, using AJAX or AHAH, so that the users can see it, and edit if necessary, not just updating the field after form is created.
Drupal 7:

Create a custom module. This module should implement hook_node_insert and hook_node_update. Both functions will have almost identical code, and it should go something like this:

/**
 * Implements hook_node_insert()
 */
function mymodule_node_insert($node) {
  if ($node-&gt;type == 'my_node_type') {
    // Set the $value variable with value you wish
    // to insert to the referenced node.
    $field = field_get_items('node', $node, 'field_whatever_it_is');
    $value = $field[0]['value'];

    // Grab the referenced node's nid.
    $field = field_get_items('node', $node, 'field_nodereference_field_name');
    $ref_nid = $field[0]['value'];

    // Load the referenced node into a variable. Avoid cached nodes.
    $ref_node = node_load($ref_nid, NULL, FALSE);

    // Update the referenced node with $value
    $ref_node-&gt;field_to_update[0]['value'] = $value;

    // Save the referenced node
    node_save($ref_node);
  }
}


If you just want to update the referenced node's node reference field with the current node's nid then you can also just use a module called Corresponding Node References