1

given a form like this:

function awesome_form() {

 $form['select'] = array(
     '#type' => 'select'
     '#options' => array(
        'hello' => t('Hello'),
        'good-bye' => t('Good Bye'),
     ),
     '#ajax' => array(
        'callback' => 'awesome_ajax',
        'progress' => FALSE,
     ),
 );

 $form['actions'] = array('#type' => 'actions');
 $form['actions']['submit'] = array(
   '#type' => 'submit',
   '#value' => t('Save'),
 );

 return $form;

}

I want the ajax callback on the select box to fire a submission. So for instance, when someone selects "Hello" the form submits and when someone selects "Good Bye" the form submits. Right now (with the code above), awesome_ajax() fires like it's supposed to, but I can't seem to get the form to submit (save the data in the database).

Also, I would like it if there wasn't a visible submit button on the page (as the submit button is unnecessary).

How would I do this?

thanks!

2

You've not mentioned what your ajax callback is doing internal to it, but if all you want is to submit the form upon a change, you can do something like this in jQuery with no drupal involvement:

$('#edit-submit').hide();
$("#edit-field-FIELDNAME-0-value").live('change', function() {
  $('#edit-submit').click();
});

where #edit-submit is the id= in the form html of the submit input button and #edit-field-FIELDNAME-0-value is the id= of your select that you want the form submited upon change. Throw this and the appropriate drupal behavior wrapper into a js file and you can drupal_add_js() it from your form hook.

ADDITION:

I suppose $('#form-id').submit(); is better than $('#edit-submit').click(); above where #form-id is the id= of your form.

Also, if you are running D6, you'll need the jQuery Update module installed.

0

If there was no submit button there, I would normally make an ajax call when the selection field is changed, pass the data to a menu callback and had it's function do the saving to the database

$('select').change(function() {
  if ($(this).text() == 'selection 1') {
       $.ajax({
          type: 'POST',
          url: '?q=modulename/save_data',
          data: 'data=' + $(this).text(),
          success: function() {
             // do other stuff..
          }
       });
  }
});

For the menu callback on my custom module, it would be like

function modulename_menu() {
  $items = array();
  $items['modulename/save_data'] = array(
      'page callback' => 'modulename_save_data',
      'access arguments' => array('administer nodes'),
      'type' => MENU_CALLBACK
  );

  return $items; // make sure to clear your drupal cache in order for this menu callback to work
}

function modulename_save_data() {
    $data = $_POST['data'];
    // do the rest here...
}

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.