1

I am constructing a form that is handled via Ajax. I need it to be avaible on browser where javascript isn't activated. For now, the submit part is like :

$form ['submit'] = array (
  '#type' => 'button' ,
  '#ajax' => array (
      'callback' => 'MODULE_form_ajax_callback',
      'wrapper' => 'result-div' 
  ),
  '#value' => t ( 'Launch' ),
  '#weight' => 10000 
);

Has anyone resolved a similar issue ?

Thanks,

  • You can have both versions, just add submit handler, but ajax without javascript? – Paul Bönisch Nov 2 '15 at 9:59
  • I need both to be working. If someone visits the website using javascript or not. For now, only those who use JS can submit the form. Sorry, my english is a bit rotten. – Ismail H Nov 2 '15 at 10:03
  • 1
    try to change '#type' => 'button' to '#type' =>'submit' and then implement hook_form_submit() – Paul Bönisch Nov 2 '15 at 10:07
1

try to change '#type' => 'button' to '#type' =>'submit' and then implement hook_form_submit()

1

Drupal delivers the submit elements with "nojs" and the submit works without ajax. If jquery is available, the first thing ajax does, is to replace the "nojs" with "ajax" to activate itself:

ajax.js:
Drupal.ajax = function (base, element, element_settings) {
  var defaults = {
    url: 'system/ajax',
    event: 'mousedown',
    keypress: true,
    selector: '#' + base,
    effect: 'none',
    speed: 'none',
    method: 'replaceWith',
    progress: {
      type: 'throbber',
      message: Drupal.t('Please wait...')
    },
    submit: {
      'js': true
    }
  };

  $.extend(this, defaults, element_settings);

  this.element = element;
  this.element_settings = element_settings;

  // Replacing 'nojs' with 'ajax' in the URL allows for an easy method to let
  // the server detect when it needs to degrade gracefully.
  // There are five scenarios to check for:
  // 1. /nojs/
  // 2. /nojs$ - The end of a URL string.
  // 3. /nojs? - Followed by a query (with clean URLs enabled).
  //      E.g.: path/nojs?destination=foobar
  // 4. /nojs& - Followed by a query (without clean URLs enabled).
  //      E.g.: ?q=path/nojs&destination=foobar
  // 5. /nojs# - Followed by a fragment.
  //      E.g.: path/nojs#myfragment
  this.url = element_settings.url.replace(/\/nojs(\/|$|\?|&|#)/g, '/ajax$1');
  this.wrapper = '#' + element_settings.wrapper;

So ajax in drupal works like you want out of the box.

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.