Multiple instances of same form in views do not work properly I have a funny problem with Drupal 7 and Views.

I created programatically a simple form with two submit buttons (yes / no). This form is displayed on a full view of a node and when the 'yes' is pressed, it goes to a 'review' form which autofills the title with the one from the node. It works very well!

function mymodule_form($form, &amp;$form_state,$nodeid) {
  global $user;
  $form['nodeid'] = array(
    '#type' =&gt; 'value',
    '#value' =&gt; $nodeid,
    '#access' =&gt; false,
  );

  $form['yes_button'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Yes'),
    '#submit' =&gt; array('yes_button_submit'),
  );

  $form['no_button'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('No'),
    '#submit' =&gt; array('no_button_submit'),
  );
  return $form;
}

//Submit handlers
function yes_button_submit($form, &amp;$form_state) {
    $nodeid = $form_state['values']['nodeid'];
    drupal_goto('node/add/review', array('query'=&gt;array('id'=&gt;$nodeid)));
}


I decided to add the buttons on the teasers that I display in a Views block. I call the form through a function which is called via a Global Php field. Here is the code inside the Global Php field, having added 'content: nid' as a hidden field in the view:

&lt;?php
  $bling=mymodule_answers($row-&gt;nid);
  echo $bling;
?&gt;


With the bit of code from mymodule_answers:

function mymodule_answers($nodeid) {
  $theform3 = drupal_get_form('mymodule_form',$nodeid);
  $output .= '&lt;p class="text-center over"&gt;'.drupal_render($theform3).'&lt;/p&gt;';

  return $output;
}


My problem is that if the view is something like this:


Node 1
Node 2
Node 3
Node 4


If I press 'yes' on any of the node teasers 2 to 4, then the review form is filled with the title of Node 1... meaning, the one at the top of the list!

I have thought about creating uniquely named forms, but I wouldn't know how to automatically generate the differently named submit handlers. Any ideas?
After looking for a while on the Internet, I found a solution. 

Firstly, you can add a '#name' to your submit buttons to distinguish them, and then do a switch case in the submit handler... I was trying this for a while before realising that it was not my problem.

My problem consists in the fact that I am calling the same form multiple times on the same page. I found a previous post which highlighted the fact that each form should get a unique form_id in order for it to work. Using hook_forms was the best method for this. I followed the instructions from here. It worked despite it being for Drupal 6.

Amended code...
I added this hook:

function mymodule_forms($form_id,$args){
  //Check if the form_id passed to drupal_get_form() contains the string 'mymodule_form'
  if (strpos($form_id, 'mymodule_form') !== FALSE) {
     //Lets attach mymodule_form to $forms[$form_id].  This effectively allows you to use the same form builder function to build a form with any form_id of your choice.
     $forms[$form_id] = array(
       'callback' =&gt; 'mymodule_form',
     );
     return $forms;
  }
 }


And then, the code that calls the form was amended to this:

function mymodule_answers($nodeid) {
   $theform3 = drupal_get_form('mymodule_form_'.$nodeid,$nodeid);;
   $output .= '&lt;p class="text-center over"&gt;'.drupal_render($theform3).'&lt;/p&gt;';

   return $output;
}


Voila!