views:

95

answers:

4

I'm using javascript to validate the form,

but can't handle the case when the form is submitted before DOM is ready

I tried :

<form method="POST" disabled="disabled">
<input type="submit" />
</form>

But the form can still be submited.

A: 

Use jQuery to create the submit button on DOM load

$(document).ready(function() {
    $("form").append("<input type='submit' name='submit' value='submit'>");
}

This will however create a dependency on javascript for your application

Or another option is to place the following code

<form action="post.php" method="post" onsubmit="return false">
wiifm
No,user can sbumit the form by pressing "Enter"
Mask
pretty sure the form will not submit if there is no submit button. Using the top solution of mine will ensure the DOM is loaded before the submit button is created
wiifm
+2  A: 

One braindead way would be to keep the submit button (or form) disabled and enable it within the window or the body's onload event. E.g:

window.onload = function() {
  document.getElementById("mySubmit").disabled = false;

  //or maybe
  document.forms[0].disabled = false;
}
karim79
A: 

Check this blog entry, it describes how to check for DOM readiness. Then, create a function which returns true when DOM is ready and use this function inside your form tag:

<form onsubmit="return my_DOM_ready_function()">
Anax
+1  A: 

Just a check before submit just to prevent a post before Dom ready.

<form method="POST" onsubmit="return handle()">
    <input type="submit" />
</form>

<script>

   function handle()
   {
       return imReady;
   }

   var imReady = false;
   function setReady()
   {
      imReady = true;          
   }

   // edit: add this line
   window.onDomReady = setReady();
</script>
Ismael
where did the `onDomReady` property on the `window` object come from?
Russ Cam