Jump to content

js causes form submit

Recommended Posts

I am not a accomplished js programmer. I am trying to get

JS to fill a input box with today's date when a button is clicked.

I am using Google Chrome. For some reason the code causes

a form submit which I do not want because I would like other

form input fields to be able to be filled in. I have done quite a

few other js forms and never had this problem.

What am I doing wrong?



<?phpecho "<HTML>  <head>       <script type='text/javascript'>      function bankdate() {        document.getElementById('bankdate').value=Date();      }          function invoicedate() {        var today = '2015-01-01';        document.getElementById('invoicedate').value=today;      }    </script>      </head>  <body>  <form name='myform' action='some.php' method='get'>   <input type='text' id='bankdate'    name='bankdate'     class='tcal' size='10'  value=''>   <button onclick='bankdate();   return false;'>Today</button><br>Bank date</br>   <input type='text' id='invoicedate' name='invoicedate'  class='tcal' size='10'  value=''>   <button onclick='invoicedate();return false;'>Today</button><br>Invoice date </br>   <br>   <button type=submit>save</button>                                                                    <!-- save button -->   <button type=button onclick="javascript:window.location='index.php';">cancel</button>        <!-- cancel button -->   <button type=button onClick="window.location='" . $_SERVER['PHP_SELF'] . "'">clear</button></td>   <!-- clear button --> </form><br><br> </body></html>";?>
Edited by jwzumwalt
Link to post
Share on other sites

I'm suspecting the problem is that you need type="button" in those buttons, but also I would change the overall format of what you have here. You are making the situation more difficult than is necessary by using the single echo. Why not just have Php inside the one line where you use it?

<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>

...but actually I don't see the need for that usage anyway. Why not just use reset?

<button type='reset'>clear</button>  

...there is a naming conflict as Dsonesuk describes below. Also there is a /br and a /td that should be removed.

Link to post
Share on other sites
That fixed it! Thanks :)(I am still baffled why this caused a form submission...)


Because of using function name that is identical to name and id reference, a error is produced, a button element with script error and also being a button that does by default act as a submit button does not carry out the 'return false' instruction due to this error.Maybe better option to use input of type button instead.

Edited by dsonesuk
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...