I am working on a custom drupal module that would change a node's node reference field value dynamically if a user clicks on an AJAX button.
In my callback function I could load the two nodes, A and B, where A has a node reference CCK field named 'foobar'.
How can I set edit and save this field? This is my attempt, but if I refresh the node, the field doesn't get updated.
$A_node = node_load($a_nid);
$B_node = node_load($b_nid);
$A_node->field_foobar[0]['nid'] = $b_nid;
node_save($A_node);