1

I want a button that runs a callback function using the AJAX method included in Form API. The button I have still submits the whole node edit form. I don't want it to do that - just pass a couple variables to the callback function and display the results. How can I do that?

This is my FAPI code for that button:

    $form['rid_import']['rid_get'] = array(
                '#type' => 'button',
                '#value' => t('Import'),
                '#attributes' => array(
                    'id' => array('fetch_rid'),
                    'class' => array('col-sm-3', 'col-xs-4')
                ),
                '#parents' => array('rid_import'),
                '#limit_validation_errors' => array(),
                '#executes_submit_callback' => false,
                "#submit" => array('sql_no_submit'),
                '#ajax' => array(
                    "callback" => 'sql_fetch_restid',   
                    "wrapper" => 'rid_sql_query',
                    "event" => 'click'
                )
          );

EDIT

You're right, I was being unclear. Basically what I wanted is something that works like your "Add More" or "Remove" buttons when you have multiple-value fields, but just to run my function without reloading the page – grab some data from the DB and show it there on the node edit form. I have a multivalue field collection that should be populated with those values and then saved together with the rest of the node. That's why I don't want it to go ahead and save the node. However, if it isn't possible I guess I will have to find another way to do what I need.

And tried to add a submit function, still reloads the whole page.

1
  • I voted it unclear because. as you can see under schlicki's answer, two of us have read it, and understood it differently. Sure proof (for me, at least) it could use more clarity about OP's intents and needs.
    – Mołot
    Jun 18, 2015 at 11:26

2 Answers 2

1

Just try to use "submit" as the type and specify a custom "#submit". From the ajax_example module:

$form['add_name'] = array(
  '#type' => 'submit',
  '#value' => t('Add one more'),
  '#submit' => array('ajax_example_add_more_add_one'),
  '#ajax' => array(
    'callback' => 'ajax_example_add_more_callback',
    'wrapper' => 'names-fieldset-wrapper',
  ),
);

This does not submit the form with the default submit handler but with your own. In this case it is "ajax_example_add_more_add_one".

Hope it helps, j

5
  • It will still submit all values, whole $form_state
    – Mołot
    Jun 18, 2015 at 11:16
  • Is it because node edit forms define the actions additionally? I used that in custom forms and it works fine.
    – schlicki
    Jun 18, 2015 at 11:20
  • It works fine. It's just not quite what OP asked. Sending everything works all right - and that's the point of my answer.
    – Mołot
    Jun 18, 2015 at 11:21
  • OK maybe I misunderstood that. I thought the problem is that OP wants the node not being saved.. But in this case my answer won't help ;) Sry..
    – schlicki
    Jun 18, 2015 at 11:24
  • Or maybe I misunderstood. Let's see what others (especially OP) will say about whose interpretation is right.
    – Mołot
    Jun 18, 2015 at 11:25
1

Well, I happened to find this entry: Ajax Callback Function in Submit Button Doesnt Work

The answer was to remove the ID of the submit button. Which I did. Now at least I'm not getting a page reload.

I will mark this as solved. Thanks to Mołot and schlicki for their time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.