1

Clicking a dialog button I want to update Drupal database using jQuery .ajax() function! I want to know if in first place is secure to do so or is another method to handle the synchronous requests?

Here is the code from my module template file:

jQuery("#dialog").dialog({
  autoOpen: false,
  resizable: false,
  draggable: false,
  modal: true,
  width: 400,
  height: 'auto',
  show: {
    effect: 'fade',
    duration: 200
  },

  open: function() {
    jQuery('#dialogSaveButton').hide();
    jQuery('#dialogDeleteButton').hide();
    jQuery('#dialogDiscardButton').hide();
  },
  buttons: [{
    text: "Edit event",
    id: 'dialogEditButton',
    click: function() {
      //- redirect
    }
  }, {
    text: "Create",
    id: 'dialogCreateButton',
    click: function() {
      createEvent();
    }
  }, {
    text: "Save",
    id: 'dialogSaveButton',
    click: function() {
      if (jQuery("#dialog-event-title").val().length == 0) {
        focusElement.children("#eventTitle").text('(No title)');
      } else {
        focusElement.children("#eventTitle").text(jQuery("#dialog-event-title").val());
      }
      jQuery('#dialog').dialog('close');
      jQuery.ajax({
        type: 'POST',
        url: 'http://localhost/?q=kalendar/test',
        success: function(data, textStatus, jqXHR) {
          console.log(data);
        },
        data: 'titleValue=' + jQuery("#dialog-event-title").val()
      });
    }
  }, {
    text: "Delete",
    id: 'dialogDeleteButton',
    click: function() {
      focusElement.detach();
      jQuery('#dialog').dialog('close');
      //- Update DataBase
    }
  }, {
    text: "Discard changes",
    id: 'dialogDiscardButton',
    click: function() {
      jQuery('#dialog').dialog('close');
    }
  }]
});

And now the .module menu_hook function:

function kalendar_menu() {
    $menu['kalendar/test'] = array(
        'page callback' => 'form_kalendar_dialog_validate',
        'access callback' => true,
        '#ajax' => array(
                'wrapper' => 'testAjax',
        ),
    );
    return $form;
}
function form_kalendar_dialog_validate($form, &$form_state) {
//--
}

function form_kalendar_dialog_submit($form, &$form_state) {
//--
}

Update:

function form_kalendar_dialog($form, &$form_state){
  $form['event_title'] = array(
    '#title' => t('Event title'),
    '#type' => 'textfield',
        '#attributes' => array('id' => 'dialog-event-title'),
    '#size' => 40,
    '#maxlength' => 200,
    '#required' => false,
  );

    $form['dialogCreateButton'] = array(
        '#type' => 'button', 
        '#value' => t('Create'),
        '#ajax' => array(
            'wrapper' => 'testAjax',
            'callback' => 'form_kalendar_dialog_validate',
            'effect' => 'fade',
        ),
    );
    $form['dialogSaveButton'] = array(
        '#type' => 'button', 
        '#value' => t('Save'), 
        '#ajax' => array(
            'wrapper' => 'testAjax',
            'callback' => 'form_kalendar_dialog_validate',
            'effect' => 'fade',
        ),
    );

    return $form;
}
function form_kalendar_dialog_validate($form, &$form_state) {
    return '<div id="testAjax" title="">'.$form_state['values']['event_title'].'</div>';
}
function form_kalendar_dialog_submit($form, &$form_state) {
  //here is the function to update database
}

In my template file I use this to call the form:

echo '<div id="dialog" title="">';
    $form = drupal_get_form('form_kalendar_dialog');
    print drupal_render($form);
echo '</div>';

And my problem is with the javascript code used to update the DOM: (is not triggered all the time)

jQuery("#edit-dialogcreatebutton").bind('click',function(event) {
    createEvent();
    jQuery('#dialog').dialog('close');
});
jQuery("#edit-dialogsavebutton").bind('click',function(event) {
    if(jQuery("#dialog-event-title").val().length == 0){
        focusElement.children("#eventTitle").text('(No title)');
    }else{
        focusElement.children("#eventTitle").text( jQuery("#dialog-event-title").val() );
    }
    jQuery('#dialog').dialog('close');
});
  • Welcome to Drupal Answers. What do you mean by "secure"? What are you trying to accomplish? – Shawn Conn May 28 '16 at 16:16
  • Thank you, glad to be here! I mean by 'secure' using the available Drupal API to know that is already securized and not to come with my own functions. I try to update the database field for a title when the user edit and save the input. I will update the question now to show you what I found so far! – Johni Devo May 28 '16 at 16:49

Your Answer

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

Browse other questions tagged or ask your own question.