Hi, I have put a form on each node view ( trough nodeapi case:view) the form only has a button which should call myform_submit which will execute a query, problem is that it is executing the _submit function twice instead of only once when i click the button . Any ideas? Here is my module's code:

function recipebox_nodeapi(&$node, $op, $teaser, $page) {
  
  $item = false;
  global $user;

  switch ($op) {
    case 'view':
      if ($teaser || $user->uid == 0) {
        break;
      }

      $res = db_query('SELECT nid, uid FROM {recipebox} WHERE nid = %d AND uid = %d LIMIT 1', $node->nid, $user->uid);
      global $item;
      $item = db_fetch_object($res);

      $type = node_get_types('type', $node);
      if ($type->orig_type != 'recipe') {
        break;
      }

     break;

  }
  if ($item == false) {
    print_r($item);
    $node->content['box_form'] = array (
      '#value' => drupal_get_form('box_form', $node, $user, "add"),
      '#weight' => 20,
    );
  }
  else
  {
    $node->content['box_form'] = array(
      '#value' => drupal_get_form('box_form', $node, $user, "delete"),
      '#weight' => 20,
    );
  }


}

function box_form($context, $node, $user, $operation) {
  $message = "";

  if ($operation == "add") {
    #print ($operation . " adding" . $node . "  " . $user);
    $message = "Add to recipe's box";
  }
  elseif ($operation == "delete") {
    print ($operation . "del");
    $message = "Delete from recipe's box";

  }
  $form = Array (
    'operation' => Array ( '#type' => 'value', '#value' => $operation, ),
    'node' => Array ( '#type' => 'value', '#value' => $node->nid, ),
    'user' => Array ( '#type' => 'value', '#value' => $user->uid, ),
    'submit' => Array( '#type' => 'submit', '#value' => $message, /*'#executes_submit_callback' => TRUE,*/ ),
  );
  return $form;
}

function box_form_submit($form, &$form_state) {
   recipe_box($form_state['values']['node'], $form_state['values']['user'],$form_state['values']['operation']);
  drupal_set_message("Recipe" . $form_state['values']['operation'] ." succesfully");
  $form_state['redirect'] = sprintf('user/%d', 1 );
}

function recipe_box($node, $user, $operation) {
#die($node . $user . $operation);
  if($operation == "add") {
    db_query("INSERT INTO {recipebox} (uid, nid) VALUES (%d, %d)", $user, $node);
  }
  elseif ($operation == "delete") {
    db_query("DELETE FROM {recipebox} WHERE uid = %d AND nid = %d", $user, $node);
    #die("delete");
  }
}

Comments

john morahan’s picture

Dunno if this will solve your problem, but I would put that if/else block with the drupal_get_form() calls inside the case 'view' of the switch... just in case something unexpected is happening when it's called for the other $op's.

summit’s picture

With only this code, I cannot comment. Looking into submit function myself, and why a problem arise is related to lots of factors. Greetings, Martijn

scb’s picture

I have the same problem... I used a tpl file to insert the button in the view, though.
But the problem is the same, the submit function is executed twice on a single click...
Any ideas?