Hello Every one
the heading says it all!
1) I have created a new custom page of my own with a few textareas and textfields (Which work perfectly)
2) Now i am trying to create a simple button with a javascript call on it.

i am doing some thing like this like :

    $form['add'] = array('#type' => 'button', '#value' => t('Add'), '#name' => 'Add', '#id' => 'Add');

3) when I open the page, the button is created, but its of a 'submit' type. So when ever i click on the button the page submits,

4) I havent found any thing helpful on the drupal forum.
5) the closed solutions being :
$form['reset'] = array ('#value' => '<input type="reset" value="Reset!">');
http://drupal.org/node/73914

6) I have already tried the code given in the drupal api at: but still i get the submit type button!!.
http://api.drupal.org/api/HEAD/file/developer/topics/forms_api_reference...

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

I am very new to drupal. Please help.
Thanks in advance
Deepak

Comments

nofear’s picture

Please post the html of your button, or better the html of the whole page. :-)

http://humanopinion.org

dwees’s picture

There is a fix you can use for the form.inc file which I have used on a site before, but the accepted way to do this is by using the 'markup' type of form element.

For instance:


$markup = '<input type="button" value="mybutton" onclick="myclickfunction()" />';
$form['myformelement'] = array(
  '#type' => 'markup',
  '#value' => $markup
);

Personally I think the fix should be included as part of the core forms.inc file because I don't think it makes that much of a difference to anyone except us hard-core Javascript users (who don't give a twit about accessibility and useability).

deepakapte06’s picture

hello
Thanks for the quick response.
1) The code snippet using the "markup" works fine (in fact quiet similar to the reset button example i mentioned in my post.)

2) dwees the fix you were referring to- I modified the code as below:
form.inc line 1361 changed to:

  if($element['#type'] == 'button'){
   return '<input type="button" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ')  .'id="'. $element['#id'].'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />\n";
  }  else{
    return '<input type="submit" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ')  .'id="'. $element['#id'].'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />\n";
  }
}

I noticed one thing though : the drupal core modules at places have a few buttons defined as "button" type. But the HTML source for these buttons too show them as of type "submit".

For example : check out the Preview button:
I had to carry out the following modification to convert the preview button to type "submit"!!
node.module line 2059

 //change button to submit in preview 
  $form['preview'] = array('#type' => 'submit', '#value' => t('Preview'), '#weight' => 40);
 

The above modification works fine at first glance. (But there might be more buttons to change. Especially in the contributed modules).
Personally, I dont like the modification (of core entities) but it works!.

Am I missing some thing here?! Or drupal doesnt provide a simple button?! Or the instruction given in the API documentation is incorrect?
Man I AM CONFUSED!!!
More ideas are welcome.

deepakapte06’s picture

hi
check this out for yourself!

check out the code in node.module at around line 2059
it should look some thing like..
$form['preview'] = array('#type' => 'button', '#value' => t('Preview'), '#weight' => 40);

Right?
now try the source of any node add/edit page. Check out the source of preview button.

<input type="submit" name="op" id="edit-preview" value="Preview" class="form-submit" />
strange? isnt it!

bendohmv’s picture

Why not use theme_button (or for me, since I'm using the Smarty engine, button.tpl) to format it?

This worked for me:

function theme_button($element) {
    if (isset($element['#attributes']['class'])) {
	$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
    }
    else {
	$element['#attributes']['class'] = 'form-'. $element['#button_type'];
    }
    
    return 
	'<button type="'. $element['#button_type'] .'" '. 
	drupal_attributes($element['#attributes']) . ' ' . 
	(empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ') .
	'id="'. $element['#id'] .'">'.
	'<span>'. check_plain($element['#value']) .'</span>'.
	'</button>\n';
}

This turns all buttons into <button> elements which are much more flexible in terms of CSS.