2

Ugh, this is probably something simple, but it is driving me crazy. I've got a simple form (just a submit button) that I am inserting into a node using hook_nodeapi(). It gets inserted correctly, and the page refreshes when I submit, but it is never hitting the form_submit() function. Here's the code:

function fantasy_stocks_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  $form = drupal_get_form('fantasy_stocks_buy_me_form', $node);
  switch ($op) {
    case 'view':
      $node->content['body']['#value'] .= $form;
      break;
  }
}

function fantasy_stocks_buy_me_form(&$form_state, $node) {
  $form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Buy') . ' ' . $node->title,
    '#description' => t('Add') . ' ' . $node->title . ' ' . t('to your stock portfolio.'),
    '#value' => t('Buy') . ' ' . $node->title,
    '#submit' => TRUE
  );
  $form['node_added'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid
  );
  $form['#submit'][] = 'fantasy_stocks_buy_me_form_submit';
  return $form;
}


function fantasy_stocks_buy_me_form_submit( $form, &$form_state ) {
  $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values,true) . '</pre>';
  drupal_set_message(t($message));
}

I've tried adding an echo and die() in the submit function, it is definitely not getting called. I've also tried leaving off the $form['#submit'] declaration, as the default should take care of it, but to no avail. I know I must be missing something stupid. Any ideas?

Also, one thing that seemed weird is that the form gets rendered with the following tag:

<form action="/MLMBid/node/5"  accept-charset="UTF-8" method="post" id="fantasy-stocks-buy-me-form-1"> 

Is that normal, to have the "-1" appended to the form id?

divibisan
  • 11,659
  • 11
  • 40
  • 58
SenorPuerco
  • 871
  • 3
  • 9
  • 19

2 Answers2

5

Finally figured it out. The part that was creating the submit button:

$form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Buy') . ' ' . $node->title,
    '#description' => t('Add') . ' ' . $node->title . ' ' . t('to your stock portfolio.'),
    '#value' => t('Buy') . ' ' . $node->title,
    '#submit' => TRUE
  );

I copied parts of this from a tutorial, and apparently the line

'#submit' => TRUE

should not be there. It was overriding the form submit handler, which made drupal attempt to look for a function called TRUE. Knew it was something stupid.

SenorPuerco
  • 871
  • 3
  • 9
  • 19
-2

I would tend to think that the "-1" on the form id is the root of your problem. However, not just the "-1" why is the form id being rendered with "-" instead of "_" like is being referenced in the rest of the code. Solve that and your problem should be fixed.

Unfortunately, I haven't used Drupal yet (just Joomla). I would try changing the code to match what the form id is being rendered as (fantasy-stock-buy-me-form-1) instead of what you currently have.

mFontenot
  • 77
  • 1
  • 6
  • 1
    I can't change the function names, dashes are an illegal character in a function name. I'm guessing drupal converts the underscores to dashes when it renders the form, and then converts them back to call the function? But yeah, the "-1" is weird...I'll look more into why that is appearing. – SenorPuerco Mar 12 '11 at 04:31
  • The -1 is there to ensure the ID is unique in case the same form might be rendered multiple times on a page. – Chris Burgess Apr 26 '12 at 22:08
  • 3
    funny enough the answer got -1 – B2F Sep 05 '13 at 15:03