Prevent form regeneration when a callback function is called through ajax Here is the code for the custom form with a field(field_B) that calls an  function through ajax. 

The issue is that I want the function (MY_MODULE_XYZ_function()) to be executed only once, when the page loads for the first time. But the function(MY_MODULE_XYZ_function()) is being executed for each ajax call through field_B because the whole form is being regenerated.

How can I prevent whole form regeneration(so that the function won't be called more than once) for each ajax call? Thanks in advance.



function MY_MODULE_menu(){
    $items['abcd/xyz'] = array(
        'title' =&gt; 'ABCD',
        'page callback' =&gt; 'drupal_get_form',
        'page arguments' =&gt; array('xyz_custom_form'),
        'access arguments' =&gt; array('access content'),
        'type' =&gt; MENU_CALLBACK,
    );
    return $items;
}

function xyz_custom_form($form, &amp;$form_state){

    MY_MODULE_XYZ_function($arg1, $arg2);

    $form['field_A'] = array(  .... );

    $form['field_B'] = array(
        '#type' =&gt; 'radios',
        '#options' =&gt; array("a"=&gt;"A", "b"=&gt;"B", "c"=&gt;"C"),
        '#prefix' =&gt; "&lt;div id='customDiv'&gt;",
        '#suffix' =&gt; "&lt;/div&gt;",
        '#ajax' =&gt; array(
        'callback' =&gt; 'MY_MODULE_custom_ajax_callback',
            'wrapper' =&gt; 'customDiv',
            'progress' =&gt; array( 'type' =&gt; 'throbber', 'message' =&gt; NULL),
            'effect' =&gt; 'fade',
        ),
    );
}

function MY_MODULE_XYZ_function($arg1, $arg2){
    //Some Query Execution
}

function MY_MODULE_custom_ajax_callback($form, &amp;$form_state){
    //Some Callback code
}
If you need a entity (which could be a node) to be created whenever the form is loaded, can create the enitiy, and pass it to your form, rather than creating it within your form. Then you can also use the same form for editing the entity.

For example, in a module called nodeform:

function nodeform_menu() {
  $menu['nodeform/add'] = [
    'title' =&gt; 'Nodeform add',
    'page callback' =&gt; 'nodeform_add_page',
    'access arguments' =&gt; ['some_create_permission'],
  ];

  // Note: using %node will call node_load(), passing it
  // the value given in that index of the path. If no node
  // is loaded, or the user does not have access, a page
  // not found error is returned (or maybe access denied)
  $menu['nodeform/%node'] = [
    'title' =&gt; 'Nodeform add',
    'page callback' =&gt; 'nodeform_edit_page',
    // The page argument is the 1st index of the path
    // which is %node. So the page argument will be a
    // node object.
    'page arguments' =&gt; [1],
    'access arguments' =&gt; ['some_edit_permission'],
  ];

  return $menu;
}

function nodeform_add_page() {
  global $user;

  $node = (object) array(
    'uid' =&gt; $user-&gt;uid,
    'name' =&gt; isset($user-&gt;name) ? $user-&gt;name : '',
    'title' =&gt; t('Default title'),
    'type' =&gt; 'some_node_type',
    'language' =&gt; LANGUAGE_NONE,
  );

  return [
    '#prefix' =&gt; '&lt;div id="nodeform_add_form_page"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    'form' =&gt; drupal_get_form('nodeform_default_form', $node),
  ];
}

// The edit page receives a loaded node as its argument:
function nodeform_edit_page(array $node) {
  return [
    '#prefix' =&gt; '&lt;div id="nodeform_edit_form_page"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    'form' =&gt; drupal_get_form('nodeform_default_form', $node),
  ];
}

// The form then receives a node:
function nodeform_default_form($form, $form_state, $node) {
  // Get the node from the form state if it exists, or use
  // the node passed to the form if it does not.
  $node = isset($form_state['nodeform_node']) ? $form_state['nodeform_node'] : $node;
  // Save the nodeform to the node
  $form_state['nodeform_node'] = $node;

  // Add your form elements:
  $form['title'] = [
    '#title' =&gt; t('Title'),
    '#type' =&gt; 'textfield',
    '#default_value' =&gt; $node-&gt;title,
  ];

  return $form;
}

function nodeform_default_form_submit($form, $form_state) {
  $node = &amp;$form_state['nodeform_node'];
  $node-&gt;title = $form_state['values']['title'];
  node_save($node);
}


You may not need all of the above example, but if you look at the nodeform/add menu path, it shows how you can always create a node when the form is called, and it will only happen once, rather than every time the form is built. If you don't need to use it in the form, then call node_save() on the node in node_add_page().