2

I'm a newb trying to learn to create a form page. Can someone tell me why the submit button isn't working? I thought having the javascript function would fix this. Thanks

<script type="text/javascript">
function submitform()
{
document.forms["contactform"].submit();
}

</head>
<body>

        <div id="contact-form">
                       <form method="POST" id="contactform" action="bespoke-form-handler.php">
                            <div>
                                <label for="name">Name:</label>
                                <input type="text" id="name" name="user_name" />
                            </div>
                            <div>
                                <label for="mail">E-mail:</label>
                                <input type="email" id="mail" name="user_email" />
                            </div>


                            <div class="button">
                                <button type="submit" name="submit"><a href="javascript: submitform()">Submit</a></button>
                            </div>
                        </form>
<script language="JavaScript">
var frmvalidator  = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("mail","req","Please provide your email"); 
frmvalidator.addValidation("mail","email","Please enter a valid email address"); 
</script>

</body>

MSkiLLz
  • 121
  • 2
  • 2
  • 12

3 Answers3

3

The problem is the button name, since you have used it as submit, document.forms["contactform"].submit will refer to the element not the submit method so it will cause an error like Uncaught TypeError: document.forms.contactform.submit is not a function

<button type="submit" name="someothername"><a href="javascript: submitform()">Submit</a></button>

Demo: Problem, Solution

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

It makes no sense to have a <a> tag inside a <button> tag because it will prevent standard HTML form submission that's done automatically via submit buttons, just remove it.

You also do not need your first JavaScript function to submit your form, the submit button takes care of this.

user1983686
  • 123
  • 1
  • 8
0

Change your submit button to

<input type="submit" name="submit" />
Deepak Nirala
  • 826
  • 6
  • 11
  • And If you want to call a javascript function than call it by onClick="functionName()" – Deepak Nirala Jun 19 '15 at 07:51
  • I added this line below the button tag I had above and it created a new field then the old button started working. When I edited the button tag to be a "input" tag it does not work. Strange. – MSkiLLz Jun 22 '15 at 23:08