I want a button in the form like this (not a submit button but a button type button):

<input class="form-submit" name="op" value="New" type="button">

I use this code:

 $form['New'] = array(
  '#type' => 'button',
  '#value' => t('New'),
  '#weight' => 19,
);

....but I obtain a submit button like this:

<input class="form-submit" name="op" value="New" type="submit">

Where is the error?

Comments

mindless’s picture

there is no core support for type="button".. here's what I did:

array('#type' => 'markup', '#value' => '<input '. drupal_attributes(array('type' => 'button', ......)) .'/>')

greg@beargroup.com’s picture

Was just working on this for our site, here's the code using the "button" form type. used this directly in my login page template.

		    $form['submit'] = array('#type' => 'button', '#value' => t('Sign up') );
			$form['#action'] = url('user/register');  //change this to the link location
			print drupal_get_form('sign_up', $form);
		  

-greg

zick’s picture

This is nice. But I have a question here. what if I want two (or more) link buttons on one page?

I tried using the code above to define two buttons, and tried to use drupal_get_form once or twice (after each form element), but the result is the same, it will give me only one button, the first one, or two buttons but link to the same page(link for the second button).

Any idea?

thanx

zick’s picture

Oh, I figured it out. Just put those codes in a function and call that function twice.

The only thing is how to make the buttons side by side?

Zick

somebodysysop’s picture

You figure out how to get those buttons side by side? It's exactly what I wish to do. Could you share the code? Thanks.

dwees’s picture

There is a patch to the forms API which allows for the button type, you could apply it, then use regular theming functions on your form button elements.

Dave

bharanikumariyerphp’s picture

Drupal 5 am using

Hi u used ur code, but when i see the view source,,
button not created ,, only submit is create

 $form['submit'] = array(
       '#type' => 'button',
       '#value' => t('submit'),
	       '#attributes' => array('onclick' => "javascript:get(this.parentNode);"),
      );
	
	 $form['clear'] = array(
    '#name' => 'clear',
    '#type' => 'button',
    '#value' => t('Reset'),
    '#attributes' => array('onclick' => 'this.form.reset(); return false;'),
  );
    $form['submits'] = array('#type' => 'button', '#value' => t('Sign up') );

View source

</div>
<input type="submit" name="op" id="edit-submit" value="submit"  onclick="javascript:get(this.parentNode);" class="form-submit" />
<input type="submit" name="clear" id="edit-clear" value="Reset"  onclick="this.form.reset(); return false;" class="form-submit" />
<input type="submit" name="op" id="edit-submits" value="Sign up"  class="form-submit" />
<input type="hidden" name="form_id" id="edit-ajaxForm" value="ajaxForm"  />

quickshare’s picture

Why have the 'button' #type listed as an element in the documentation when its not even supported in core?

dwees’s picture

Someone/I created a patch for this, but I forget where it is. It actually isn't all that hard to fix.

Not sure why it hasn't been fixed.

Dave

Firewolf’s picture

A patch is nice of course, but if the patch is not implemented, it's not much of use to implement a 'button' type button in a module. Then everybody who wants to use the module needs to patch the core.

Is there a reason why this button type button is not implemented? Even in the 'Pro Drupal Development' book this button type button is mentioned on page 182.

But anyway, it is not working. I am using Drupal 5.2 and my button type buttons are translated as submit buttons. I am getting headache to try to find a workaround for the feature I want to implement in a module. I can use a link but that would just not look nice.

TapocoL’s picture

Nevermind, my idea causes problems elsewhere.

mattconnolly’s picture

I'll add another vote for a #type -> button to be actually a <input type=button and not a <input type=submit.

I'd also like to be able to assign javascript actions as attributes without having all the "s turned into "s.

Ryan Palmer’s picture

I came across this today and filed a bug report:

#289240: Allow #type 'button' that aren't form submits

zfreaker’s picture

When pressing a button form gets submit to Drupal, but the submit handler is not invoked. Weird behavior. Why not leaving the type button as it is? I am confused.

Chandan Chaudhary’s picture

You can do it by simply overriding the button theme in your theme template.php file

function THEME_NAME_button($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = $element['type'];
  element_set_attributes($element, array('id', 'name', 'value'));

  $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  if (!empty($element['#attributes']['disabled'])) {
    $element['#attributes']['class'][] = 'form-button-disabled';
  }

  return '<input' . drupal_attributes($element['#attributes']) . ' />';
} 

Need Drupal help?
Reach me

Acquia Certified Developer

Backend Frontend and DevOps.

anybody’s picture

http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes