1

I have a form which I want to submit on bodyLoad.

I have written document.forms[0].submit() which works fine with IE 9 and Chrome 14 but same is not working in FF 3.6.23.

Any ideas? Is this a known issue with FF?

I have tried other options like document.form_name.submit() and document.getElementById('form_id').submit() but nothing works with FF.

The error I am getting in FF is

document.forms[0] is undefined

This is what I have written in the view (CakePHP 1.2.6):

<?php $this->layout = 'blank'; ?>

<?php e($form->create('Mymodel', array('name'=>'myform', 'url'=>'gohere'))); ?>

<?php e($form->hidden('name', array('value'=>$name))); ?>

<?php e($form->end()); ?>

<script language="javascript">
document.forms[0].submit();
</script>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
dev
  • 13
  • 5

2 Answers2

1

That won't work in Firefox, since the page isn't fully loaded yet.

Eliminate

document.forms[0].submit();

and set

<body onLoad="document.forms[0].submit();">

Be careful not to point <form> to the current page, as it would cause an endless loop.

Dennis
  • 14,264
  • 2
  • 48
  • 57
  • Thanks Dennis. Is this documented somewhere in FF. I mean where can I find all such know issues for FF, etc. ? – dev Oct 07 '11 at 08:20
  • I was wrong. I've changed my answer to display the actual facts. For bugs in Firefox, visit [Bugzilla](https://bugzilla.mozilla.org/). – Dennis Oct 07 '11 at 08:22
  • okie... so the setTimeout() thing worked because the form is loaded by that time. thanks again – dev Oct 07 '11 at 09:32
0

The javascript code may be executed before the form is actually loaded. That's why Firefox can't find document.forms[0]. You have to make sure your page is ready before executing javascript. With JQuery, you do it with $(document).ready()

WilQu
  • 7,131
  • 6
  • 30
  • 38
  • Thanks WilQu. I will check how to do this without jQuery [ because i am not using it ]. – dev Oct 07 '11 at 09:31