Drupal form ajax submit not working - ajax

The drupal form that i submit has ajax property defined
'#submit' => array(),
'#ajax' => array(
'callback' => 'form_submit',
'wrapper' => 'problem-wrapper',
'method' => 'replace',
'effect' => 'fade',
$form['#validate'][] = 'problem_form_validate';
but it does the validation only on second click of the submit button. The first time i submit it shows the please wait...
throbber but nothing happens. The second time I click the button it works. What could be the possible reason. This is happening on a specific environment. on some other environment it is not and issue.
What could possibly be in the settings of an environment that forces the validate function to be called only on the second submit.
Thanks,

Related

How create and ACF form with preview button?

I hava a front end form i want to have a preview button for the form. problem is acf form generates the post id after submitting the form. So can't add a preview link
get_preview_post_link this can be used to get the preview link but it requires a iD to passed..
How can i add a preview button? may be ajax save the post as draft? and allow option to publish in the new page?
<?php
acf_form(array(
'post_id' => 'new_post',
'submit_value' => __("Create", 'acf'),
'return' => '%post_url%',
'updated_message' => __('added Successfully', 'acf'),
'post_title' => true,
'post_content' => false,
'new_post' => array(
'post_type' => 'l',
'post_status' => 'publish',
),
));
?>
Please help thanks

magento admin confirmation popup on invoice creation

I am trying to add a confirmation popup on to a existing button in the Magento Admin. When you press capture it shout ask if you are sure. I have found the code of the button in the adminhtml folder and dont know further. I have found out that you can use the onclick parameter to start a js function but that parameter is already set by magento.
Here is the code of the button
if ($this->_isAllowedAction('capture') && $this->getInvoice()->canCapture()) {
$this->_addButton('capture', array(
'label' => Mage::helper('sales')->__('Capture'),
'class' => 'save',
'onclick' => 'setLocation(\''.$this->getCaptureUrl().'\')'
)
);
}
'onclick' => "confirmSetLocation('Are you sure you want to do this?', '{$this->getUrl('*/*/delete')}')",
You have to replace 'onclick' => 'setLocation(\''.$this->getCaptureUrl().'\')'
with 'onclick' => "confirmSetLocation('{$message}', '{$this->getCaptureUrl()}')",

drupal ajax call from click of a link in form

How do I submit a form using AJAX when clicking a link in Drupal?
function search_form($form,$form_state) {
$form['support_email'] = array(
'#theme' => 'link',
'#text' => '',
'#ajax' =>array(
'callback' => 'ajax_change',
'wrapper' => 'email-hidden',
'method' => 'replace',
'click' => 'true',
),
);
}
This is the form link I have inside a form. On clicking the link, I want the AJAX callback function ajax_change to be called, which does not seem to be happening.
The forms api reference for the #ajax functionality says that it is "Used by: button, checkbox, checkboxes, image button, password, radio, radios, select, submit, tableselect, textarea, text_format, textfield". Link is not in the list and thus won't work. The #ajax functionality makes Drupal perform an AJAX call when the specified form element changes. Since a link doesn't change, it is logical that it doesnt work.
The Asaf (ajax submit for any form) module may be able to help you to achieve what you want to do.
Also, it appears you are trying to use this AJAX to hide an element. The Forms API has a states functionality that makes it easy to conditionally show/hide elements. Read this for more info about states.
The #ajax callback is used to make a form that dynamically changes itself.
In Drupal it's not possible (without using third party modules) to use link tag as an AJAX triggering element. The alternative solution may be to create a hidden button element with AJAX functionality, and make link trigger click event on that hidden button.
Here is form function:
function mymodule__form($form, $form_state) {
// JS file contains the code for triggering click event on e hidden button.
$form['#attached']['js'][] =
drupal_get_path('module', 'mymodule') . '/js/mymodule.behaviors.js';
// Our link element.
$form['link_mymodule'] = array(
'#type' => 'link',
'#name' => 'link_mymodule',
'#title' => t('Perform mymodule logic'),
'#href' => current_path(),
'#options' => array(
'attributes' => array(
'class' => array('mymodule_ajax'),
),
),
);
// Hidden AJAX enabled submit button.
$form['mymodule_ajax_submit'] = array(
'#type' => 'button',
'#value' => 'AJAX submit',
'#name' => 'mymodule_ajax_submit',
// You may need this to disable validation.
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => '_mymodule__form__pager_callback',
'event' => 'click',
),
'#attributes' => array(
// Class "element-hidden" will hide the button.
'class' => array('element-hidden', 'mymodule_ajax_submit'),
),
);
// Some element for tests.
$form['random_thing'] = array(
'#type' => 'markup',
'#markup' => rand(1, 10000),
// Wrapper is needed since AJAX will use it as a container for incoming data.
'#prefix' => '<div class="ajax_wrapper">',
'#suffix' => '</div>',
);
return $form;
}
You will also need callback function for replacing old data with new one since you are using AJAX:
function _mymodule__form__pager_callback($form, &$form_state) {
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace('.ajax_wrapper', trim(render($form['random_thing']))),
),
);
}
Also you will have to attach click event to your link, which will trigger click event on a hidden button. That's what stored in /js/mymodule.behaviors.js file.
(function ($, Drupal) {
Drupal.behaviors.mymodule = {
attach: function (context, settings) {
// Since behaviors will be executed every times AJAX is called, it's better to use $.once() method even if you
// are going to use "context" argument. That is needed to be sure that event is attached only once.
$('.mymodule_ajax', context).once('mymodule_ajax', function () {
// Bind click event to out link.
$(this).click(function (e) {
// Prevent browser to follow the link.
e.preventDefault();
// Perform click triggering.
$('input.mymodule_ajax_submit').click();
});
});
}
}
}(jQuery, Drupal));

Is there a way to speed up ajax calls in Drupal forms

I've built a form in Drupal 7 using form API and ajax calls. A typical form item looks like this:
$form['wrapper']['step1']['currency'] = array(
'#type' => 'radios',
'#options' => array(
'USD' => t('USD'),
'GBP' => t('GBP'),
'EUR' => t('EUR'),
),
'#default_value' => (!empty($form_state['values']['currency'])) ? $form_state['values']['currency'] : 'USD',
'#title' => t('Choose Currency'),
'#required' => TRUE,
'#ajax' => array(
'callback' => 'ajax_step1',
'wrapper' => 'step1-wrapper',
'method' => 'replace',
'effect' => 'fade',
'speed' => 'fast',
),
);
Everything is working as should but even when the ajax call just rebuilds a small part of the form it takes couple of seconds (the throbber is working overtime :).
Is this normal?
Is there a way to speed this (keeping things the Drupal way)?
When the ajax call is made, it literally rebuilds the entire form and returns only an aspect of it. For instance, in your function ajax_step1, you are probably calling to return a certain element from the form, and display it in the step1-wrapper div/wrapper element.
If you want to speed up the form return, you need to optimize the form builder itself. Which means, you might need to rewrite the form.

Place an image for submit button in Zend form

I have a form made by Zend Framework that needs a submit button. I want to place an image instead of the default button. The code I have is this:
$submit = new Zend_Form_Element_Submit( 'submit');
However, I can not add an image for the submit button here. If I create the form with the following code, it works:
$this->addElement('image', 'submit', array(
'ignore' => true,
'label'=>'Submit',
'src' => ('/media/css/image/submit.png'),
));
How can I add image instead of the default submit button in the first approach of form creation?
$submit = new Zend_Form_Element_Image('submit', array(
'ignore' => true,
'label' => 'Submit',
'src' => '/media/css/image/submit.png'
));

Resources