0

How can I have a JS confirmation button pop-up when a user clicks a button and then fire the #ajax defined for that button?

Right now, I have this button at the end of my form:

    //add an update button to the end of every row
    $form[$row_name][sizeof($data['columns']) + 1] = array (
        '#type' => 'button',
        '#value' => t('Delete Row'),
        '#name' => $row . '-_udr_delete_data',
        '#prefix' => "<td class='delete-button'>",
        '#suffix' => "</td>",
        '#ajax' => array (
            'callback' => '_udr_ajax_handler',
            'progress' => FALSE,
         ),
        '#weight' => 1001,
    );
  }
  $form['table_end'] = array(
      '#prefix' => '</table>',
  );

  //if the form has been changed, update the appropriate fields
  if ( isset($form_state['triggering_element']) ) {
    $callback = _name_to_callback($form_state['input']['_triggering_element_name']);
    if ($callback == "_udr_update_data") {
      _udr_update_data($form, $form_state, $data);
    } 
    elseif ($callback == "_udr_delete_data") {
      _udr_delete_data($form, $form_state, $data);
    }
  }
  return $form;
}

I would like to make this button make a pop-up warning that asks the user if he/she would like to really delete the data. If yes, then delete. If no, then don't delete.

I have tried various methods such as:

'#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),

And also:

    // Add an delete button to the end of every row
    $form[$row_name][sizeof($data['columns']) + 1] = array (
      '#type' => 'submit',
      '#submit' => array('udr_submit_rebuild'),
      '#value' => t('Delete Row'),
      '#name' => $row . '-_udr_delete_data',
      '#prefix' => "<td class='delete-button'>",
      '#suffix' => "</td>",
      '#ajax' => array (
        'wrapper' => "row-$row",
        'callback' => '_udr_ajax_handler',
        'progress' => FALSE,
      ),
      '#weight' => 1001,
    );
  }

  if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] == t('Delete Row')) {
    $form[$row_name][sizeof($data['columns']) + 1]['#value'] = t('Really Delete?');
    $form[$row_name][sizeof($data['columns']) + 1]['#submit'] = array('udr_submit_handler');
  }

  $form['table_end'] = array(
    '#prefix' => '</table>',
  );

  //if the form has been changed, update the appropriate fields
  if ( isset($form_state['triggering_element']) ) {
    $callback = _name_to_callback($form_state['input']['_triggering_element_name']);
    if ($callback == "_udr_update_data") {
      _udr_update_data($form, $form_state, $data);
    } 
    //~ elseif ($callback == "_udr_delete_data") {
      //~ _udr_delete_data($form, $form_state, $data);
    //~ }
  }
  return $form;
}

function udr_submit_rebuild($form, &$form_state, $data) {
    $form_state['rebuild'] = TRUE;
}

function udr_submit_handler($form, &$form_state, $data) {
  $callback = _name_to_callback($form_state['input']['_triggering_element_name']);
  if ($callback == "_udr_delete_data") {
    _udr_delete_data($form, $form_state, $data);
    $form_state['rebuild'] = TRUE;
  }
}
2
  • You've a nice code snippet.
    – niksmac
    Apr 17 '12 at 4:18
  • We opted to not delete the DB rows but mark them as deleted in a column and then have an undelete option. Cleaner. Easier. The right way to program.
    – jgspratt
    Apr 20 '12 at 3:09
3

The correct way to fix this issue is to is to attach a JavaScript watch to the button and have the trigger make a confirm dialog pop up and then make a JavaScript request from the user's browser back to a URL with a parameter of the element to be deleted at the end of it, then fade out the rows from the HTML table and make sure the zebra striping is correct, then secure that second URL to have the same permissions as the original page, then receive the parameter at the called URL and process the callback you had originally intended to run:

PHP:

onclick="Drupal.dccAdmissions_confirm_delete('$rec->pi_firstname $rec->pi_lastname', '$rec->pi_id . ', '$rec->pi_id', jQuery(this) );

JavaScript:

( function($) {
  Drupal.dccAdmissions_confirm_delete = function( in_name, id, object ) {
    if ( confirm( 'Do you want to delete the application for ' + in_name + '?' ) ) {
      $.ajax({
        type: 'GET',
        url: '/admissions/application/delete/' + id,
        success: function(data) { 
          if ( data == 'DELETED' ) {
            // Remove the specific row
            $(object).parents('tr').fadeOut( 1000 );
            $(object).parents('tr').remove();
            $('#dccadmissions-application-management-form tr:nth-child(odd)').addClass( 'odd' );
            $('#dccadmissions-application-management-form tr:nth-child(odd)').removeClass( 'even' );
            $('#dccadmissions-application-management-form tr:nth-child(even)').addClass( 'even' );
            $('#dccadmissions-application-management-form tr:nth-child(even)').removeClass( 'odd' );
          } else {
            alert( 'Delete failure: ' + data );
          }
        } 
      }); 
    }
  }
  Drupal.dccAdmissions_confirm_reset = function( in_name ) {
    return confirm( 'Do you want to reset the application for ' + in_name + '?' );
  }
})(jQuery);

Make a menu hook:

function dccAdmissions_menu() {
  $items['admissions/application_management'] = array (
    'title' => t("Admissions Online Application Management"),
    'page callback' => 'dccAdmissions_application_management_content',
    'type' => MENU_CALLBACK,
    'access callback' => 'dccInGroup',
    'access arguments' => array( 'Admissions' ),
  );

Check your cases:

/*
 * Page callback for application management pages.
 */
function dccAdmissions_application_management_content( $arg1 = 'new', $arg2 = 0, $arg3 = '' ) {
  include_once "application-management.data.inc";

  $module_path = drupal_get_path( 'module', 'dccAdmissions' );
  $output = array(
    '#attached' => array(
      'css' => array( $module_path . '/css/application-management.css', ),
      'js'  => array( $module_path . '/js/application-management.js', ),
    ),
  );

  switch( $arg1 ) {
    case 'delete':
      $message = _dccAdmissions_application_management_delete( $arg2 );
      return drupal_json_output( $message );
      break;

Then delete your data:

function _dccAdmissions_application_management_delete( $id ) {
//... your delete code here...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.