title: Drupal 7 how to prevent multiple form submissions (server side)
body: <p>Here is the working module code I am testing with:</p>
  
  <pre><code>/**
   * @file myform.module
   */
  
   /**
   * Implements hook_menu().
   */
   function myform_menu() {
     $items['myform'] = array(
       'title' =&gt; 'myform',
       'page callback' =&gt; 'drupal_get_form',
       'page arguments' =&gt; array('myform'),
       'access callback' =&gt; true,
       'type' =&gt; MENU_NORMAL_ITEM
     );
     return $items;
   }
  
   /**
   * Form
   */
   function myform() {
     $form['Value'] = array(
       '#title' =&gt; t('Value'),
       '#type' =&gt; 'textfield',
       '#description' =&gt; t('You may not enter the same value twice. (unless you hit enter really fast).'),
       '#required' =&gt; true,
     );
     $form['submit'] = array(
       '#type' =&gt; 'submit',
       '#value' =&gt; t('Submit')
     );
     return $form;
   }
  
   /**
   * Validate
   */
   function myform_validate($form, &amp;$form_state) {
     if (isset($form_state['values']['Value']) &amp;&amp; trim($form_state['values']['Value'])!=''){
       // prevent duplicates
       db_set_active('test');
       $r = db_query("SELECT id FROM test WHERE value = '".$form_state['values']['Value']."'");
       $n = $r-&gt;rowCount();
       if ($n) {
         form_set_error('Value', t('This value has already been submitted.'));
       }
       db_set_active();
     }
   }
  
   /**
   * Submit
   */
   function myform_submit($form, &amp;$form_state) {
  
     for ($i=0; $i&lt;=10000000; $i++) {
       // do nothing
     }
  
     db_set_active('test');
     db_insert('test')-&gt;fields(array('value'=&gt;$form_state['values']['Value']))-&gt;execute();
     db_set_active();
   }
  </code></pre>
  
  <p>The validation hook prevents duplicate values from being inserted, unless I hit the enter key or submit button really fast in which case the same value is inserted into the database multiple times.</p>
  
  <p>How do I prevent duplicate values from being inserted?</p>