Auto form submit with javascript in Drupal

I'm sure some people know how to do this, but when i approached this task, i was confused. So:
1. Find a proper form_id of form you want to submit automatically with javascript. You can find it in html generated by Drupal
2. Use a hook_form_alter to add this elements to form. Without them, after javascript submit, Drupal FAPI do not know what to do with form.

  $form['op'] = array(
    '#type' => 'hidden',
    '#value' => t('Post comment') // The task was to autosubmit comment reply form
  );
  $form['form_id'] = array(
    '#type' => 'hidden',
    '#value' => 'comment_form' // comment_form for me and form_id you want to submit
  );

3. Then in the javascript when you call

document.getElementById('comment-form').submit();

you'll have drupal form submitted in a normal way it should.

Trackback URL for this post:

http://alexisyes.com/trackback/10

3 comments

1.  Anonymous (12 August, 2008 - 21:26) says:

Thanks a lot, man!

2.  Tera Bitez (26 January, 2009 - 07:59) says:

Nice article but how will the submit action be triggered?

3.  Anonymous (3 March, 2009 - 13:10) says:

Hello sir

we have rendred drupal form whose id is emp_information(which is a id provided by drupal itself), but when i tried to put an id as you told in above section then it does not replicating,

also on bases of id emp_information the form going to refresh on auto submit call. but it is not inserting any value to database.

please help me

thanks

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.