How can I execute a JavaScript function before the form submission handler is called? I was trying to save an image in the database using the following code.

function myid_user_page_form() { 
  $form = array();
  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Save',
    '#attributes' =&gt; array(
      'id' =&gt; 'myid_save',               
    ),
    '#submit' =&gt; array('formsave_action'), 
  );  

  return $form;
}

function formsave_action($form,&amp;$form_state) {  
  $save  = db_update('student',array('target' =&gt; 'import'))
    -&gt;fields(array('id_status' =&gt; '1'))
    -&gt;condition('stud-id', '101-03043', '=')
    -&gt;execute();

  if ($save) {
    drupal_set_message("Successfully saved.");
  }
  else {
    drupal_set_message("Unsuccessfully save.");
  } 
}


The JavaScript file content is the following.

 $('#myid_save').on('click', function(e) {               
   myid_save_signature();  
   myid_save_photo();                   
 });


In my case, the two JavaScript functions aren't called; the page refreshes before they are executed. I want to run those functions before formsave_action() is called.
How can I achieve this?
Remove #submit.

$form['submit'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; 'Save',
  '#attributes' =&gt; array(
    'id' =&gt; 'myid_save',               
  ),
);


Your submit function should be myid_user_page_form_submit rather than formsave_action. If your form's function name is myform_form, then you just need to write the submit handler like myform_form_submit. It will consider it automatically.

Now change your js to :

$('#myid_save').on('click', function(e) {               
    myid_save_signature();  
    myid_save_photo();
    $('#myid_save').submit(); // Add this line                 
});


It will submit your form using jquery.