I have a custom Contact Us form I created with WebForm. It worked fine until I implemented hook_form_alter to add an image button and created a theme template file.

My hook_form_alter looks like this:

function mymodule_form_webform_client_form_34_alter(&$form, $form_state) {
  unset($form['submit']);
  $img_path = 'sites/default/mytheme/files/images/nav/';

  $form['submit'] = array ( 
    '#type' => 'image_button',
    '#src' => $img_path . 'SubmitOff.jpg',
    '#attributes' => array(
      'onmouseover' => 'this.src=\'' . $img_path . 'SubmitOver.jpg\';',
      'onmouseout' => 'this.src=\'' . $img_path . 'SubmitOff.jpg\';',
    ),
  );	
}

I then created a webform-form-34.tpl.php file in my theme folder. The main output looks like this:

  $output = '';
  foreach(element_children($form) as $element) {
    switch($element) {
      case 'submitted':
        $req = '<span class="form-required" title="This field is required.">*</span>';
        $output = '<table cellspacing="0" cellpadding="0"><tbody>';
				
        foreach(element_children($form[$element]) as $field) {
          if($form[$element][$field]['#type'] == 'textarea') {
            $output .= '<tr><td colspan="2" style="padding-top: 15px;">' . $form[$element][$field]['#title'] . ': ';
            if($form[$element][$field]['#required']) {
              $output .= $req;
            }
            unset($form[$element][$field]['#title']);
            $output .= drupal_render($form[$element][$field]);
          }
          else {
            $output .= '<tr><td valign="middle" style="width: 90px;">' . $form[$element][$field]['#title'] . ': ';
            if($form[$element][$field]['#required']) {
              $output .= $req;
            }
            $output .= '</td>';
            unset($form[$element][$field]['#title']);
            $output .= '<td valign="middle" style="width: 185px;">';
            $output .= drupal_render($form[$element][$field]);
          }
					
          $output .= '</td></tr>';
        }
				
        $output .= '</td></tr></tbody></table>';
        break;
			
      case 'submit':
        $imgPath = constant('DEV_PATH')  . '/sites/default/whimsical/files/images/nav/';
        $resetOffSrc = $imgPath . 'ResetOff.jpg';
        $resetOverSrc = $imgPath . 'ResetOver.jpg';
        $output .= drupal_render($form[$element]);
        $output .= '&nbsp;&nbsp;';
        $output .= '<a href="javascript:void(0);" onclick="resetForm(\'webform-client-form-34\'); document.getElementById(\'edit-submitted-comments\').value = \'\';"><img src="' . $resetOffSrc . '" border="0" onmouseover="this.src=\'' . $resetOverSrc . '\';" onmouseout="this.src=\'' . $resetOffSrc . '\';" /></a>';
        break;
				
      default:
        $output .= drupal_render($form[$element]);
        break;
    }
  }
  print $output;

The form looks great, but when I submit nothing goes to the database and no email is sent. Additionally, no error message is displayed on the page or in the log.

Can anyone see anything obvious I'm missing here? Any help would be greatly appreciated.

MK

Comments

mk31762’s picture

I figured out that the image_button in the form_alter was causing the problem. I noticed that the name given to the standard submit buttton was "op". So I added a name field to my submit array like so:

  $form['submit'] = array ( 
    '#type' => 'image_button',
    '#src' => $img_path . 'SubmitOff.jpg',
    '#attributes' => array(
      'onmouseover' => 'this.src=\'' . $img_path . 'SubmitOver.jpg\';',
      'onmouseout' => 'this.src=\'' . $img_path . 'SubmitOff.jpg\';',
    ),
    '#name' => 'op',
  );

Worked like magic!