2

I have a form with 2 submit in it, one of them is a button to generate codes, everytimes I click on generate form_submited called up and a record insert in db, how can I prevent form_sumit in ajax triggering?

function rah_cc_generate_form($form,$form_state){
    $form=array();

    $form['gen']=array(
        '#type' => 'fieldset',
        '#name' =>t('generate gift card code'),
        '#collapsible' => TRUE,
        '#collapsed' => false,
//        '#weight' => 5,

    );
    $form['gen']['code']=array(
            '#type' => 'textfield',
            '#title' => t('gift code'),
            '#default_value' => '',
            '#prefix' => '<div id="gc-code">',
            '#suffix' => '</div>',
            '#size' => variable_get('rah_gc_code_size', 8),
            '#maxlength' => 20,
            //'#required' => TRUE,
    );
    $form['gen']['price']=array(
            '#type' => 'textfield',
            '#title' => t('price'),
            '#default_value' => 10000,
            '#size' => 10,
            '#maxlength' => 20,
            '#required' => TRUE,
    );
    $form['gen']['status'] = array(
  '#type' => 'checkbox',
  '#title' => t('status'),
    '#default_value' => 1,
);

    $form['gen']['random']=array(
     '#type' => 'submit',
    '#value' => t('generate'),
    '#description' => t("if you want generate random use this plz"),
    //'#weight' => 1,
    //'#submit' => array('rah_cc_generate_random'), // If no javascript action.
    '#ajax' => array(
      'callback' => 'rah_cc_generate_ajax',
      'wrapper' => 'gc-code',
      'method' => 'replace',
      'effect' => 'fade',
      ),
   );


    $form['gen']['save']=array(
        '#type' => 'submit',
        '#value' =>t('save'),
         );
    ym($form);    
    return $form;


}
function rah_cc_generate_ajax(&$form,$form_state){

    //return $form['gen']['code']['#default_value']=33;
     $form['gen']['code']['#value']=rah_cc_generate();
     //$form_state['values']['code']['und'][0]['value']=44;
     return $form['gen']['code'];

}

update I use 'prevent'=> 'sumbit', but not get answer :(

 $form['gen']['random']=array(
     '#type' => 'submit',
    '#value' => t('generate'),
    '#description' => t("if you want generate random use this plz"),
    //'#weight' => 1,
    //'#submit' => array('rah_cc_generate_random'), // If no javascript action.
    '#ajax' => array(
      'callback' => 'rah_cc_generate_ajax',
      'wrapper' => 'gc-code',
      'method' => 'replace',
      'effect' => 'fade',
      'prevent'=> 'sumbit',
      ),
   );

enter image description here

  • "submit" is misspelled in your edit, could that have been something? – akalata May 15 at 19:33
6

$form_state variable holds 'triggering_element' element which indicates what button triggered (or also you can use #clicked_button element), example (in ajax callback function):

function my_form_submit($form, &$form_state) {
  //dpm($form_state);
  if($form_state['triggering_element']['#value'] == 'MY_ELEMENT\'S_NAME') {
    //do_operation();
  }
}
  • thanks. I dsm $$form_state['triggering_element'] and findout I should compare it with #value, tnx for your big clue – Yusef Mohamadi Jan 28 '14 at 14:35
  • @zhilevan Not at all. – xurshid29 Jan 28 '14 at 14:38
  • but #name return op for me :).how over tnx for your clue. – Yusef Mohamadi Jan 28 '14 at 14:41
  • @zhilevan see here for a way I use to separate user-displayed value from application logic. – Mołot Jan 28 '14 at 15:18

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.