I need help with making a drupal form. I've made the form and it works perfectly fine, all it does is capture the email, ip, and date and time of a user. The problem is I don't want the user to have to click a button in order to submit the form, I want the form to automatically submit after a second or something.

//form hook, form elements start here

function form_casl_form($form, &$form_state) {
//sometext here
 $form['some_text'] = array(
    '#markup' => '<p><b>Simply click the button to subscribe.</b>

  //submit button
  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Subscribe'),

  return $form;
    //validate hook

Is there anyway to do this? I'm very close to finishing and this is the only thing I'm stuck on, any ideas would be appreciated!

2 Answers 2


There is no solution to auto-submit a form using only Drupal Form API. You will need to add some lightweight custom JavaScript to your form in order to achieve this. The proper way to add JavaScript to your form is to use the #attach attribute of the form.

$form['#attached']['js'][] = drupal_get_path('module', 'YOURMODULE') . '/YOURMODULE.js';

The JavaSript code itslef is pretty simply, just remember to properly wrap it in a Immediately Invoked Function Expression and to use Drupal.behaviors and jQuery Once.

Drupal.behaviors.YOURMODULE = {
  attach: function(context, settings) {
    $('.your-form-selector', context).once('YOURMODULE').delay(10000).submit();

This one is a pretty easy method by adding 'onChange' to the '#attributes' array:

 * Custom form handler.
function FORM_EXAMPLE($form, &$form_state) {

  $form['complete_projects_radios'] = [
    '#type'          => 'radios',
    '#title'         => t('Project status'),
    '#default_value' => 0,
    '#options'       => [
      0 => t('Current'),
      1 => t('Complete'),
    // Define the onChange like so:
    '#attributes'    => [
      'onChange' => 'this.form.submit();',

  $form['complete_projects_submit'] = [
    '#type'       => 'submit',
    '#value'      => t('Apply'),
    // The submit itself is necessary, but now it can be hidden:
    '#attributes' => [
      'style' => ['display: none;'],

  return $form;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.