Prevent Drupal's Page from refreshing after a button is click in Forms API

1.8k views Asked by At

I had started developing a module in Drupal. I made two form controls: a fieldset and a button. The button calls a javascript function "myphoto_options" when it is click. This function only does an alert function. After executing the called function, the page then immediately refreshes the page. How to prevent the page from reloading?

Below are my codes:

function myid_user_page_form(){  
    $form = array();
    $form['id'] = array(
        '#type' => 'fieldset',
        '#title' => t('ID Information'),
        '#collapsible' => TRUE, 
        '#collapsed' => FALSE,
    );
    $form['id']['myphoto_button'] = array(
        '#type' => 'button', 
        '#value' => '...',
        '#attributes' => array(
        'onclick' => "myphoto_options();",),  
    );  
    return $form;
}

Javascript function:

function myphoto_options(){
    alert("Hey");
    return false;
}

Any help would be very much appreciated. Thanks.

1

There are 1 answers

0
Muhammad Reda On

Set #executes_submit_callback property in your button to FALSE.

$form['id']['myphoto_button'] = array(
    '#type' => 'button', 
    '#value' => '...',
    '#attributes' => array(
    '#executes_submit_callback' => FALSE, // added line.
);