2

I want to use ajax call on image button

here is my code.

function clicktell_form_alter(&$form, &$form_state, $form_id) {

    if($form_id=='user_profile_form') {

       $form['linkb'] = array(
            '#type' => 'image_button',
            '#href' => '',
            '#title' => t('something'),

            '#name' => 'linkb',
            '#ajax' => array(
                'method' => 'html', // this is important without this if else condition for ajax will not work.
                'effect' => 'fade',
                'callback' => 'clicktell_verification',
                'wrapper' => 'replace_textfield_div',),
        );
        $form['cardnresp'] = array(
            '#type' => 'hidden',
            //'#default_value' => '',
            '#title' => t('This is where msg comes'),
            '#prefix' => '<div id="replace_textfield_div">',
            '#suffix' => '</div>',
        );
        $form['#validate'][]='clicktell_error';  


    }
    return $form;
}

and

function clicktell_error($form, &$form_state) {
  //my code 
}

function clicktell_verification($form, &$form_state) {
 //my code
}

But now when i click on image button it jst refresh the page and no ajax call happnes

Does anybody have idea regarding this

Thanks

  • Might help if you could (on a copy of course) trim your code in omitted functions to minimum amount needed to show your problem. If it works with functions empty, add that note in comments inside these functions. – Mołot Aug 23 '13 at 7:13
1
+50
function clicktell_form_alter(&$form, &$form_state, $form_id) {

    if($form_id=='user_profile_form') {

       $form['linkb'] = array(
            '#type' => 'image_button',
            '#href' => '',
            '#title' => t('something'),

            '#name' => 'linkb',
            '#ajax' => array(
                'method' => 'html', // this is important without this if else condition for ajax will not work.
                'effect' => 'fade',
                 'prevent' => 'mousedown',
                'callback' => 'clicktell_verification',
                'wrapper' => 'replace_textfield_div',),
        );
        $form['cardnresp'] = array(
            '#type' => 'hidden',
            //'#default_value' => '',
            '#title' => t('This is where msg comes'),
            '#prefix' => '<div id="replace_textfield_div">',
            '#suffix' => '</div>',
        );
        $form['#validate'][]='clicktell_error';  


    }
    return $form;
}

This will run ajax but you may feel form is submitting but there is no actual submitting of form

0

You didn't set #ajax_prevent. It should default to 'click' all right, but my experience says sometimes it does not, and sometimes it is not sufficient. Try to add this to your ajax settings:

'prevent' => 'submit click mousedown',
  • not working. Still form submits – Rashmi Aug 25 '13 at 4:28

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.