Jump to content

js causes form submit


jwzumwalt

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?

 

temp_01.png

<?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 comment
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 comment
Share on other sites

Change the function name so it does not match id or name attribute values for example id="bankdate' or name='bankdate', its a conflict.

 

That fixed it! Thanks :)

(I am still baffled why this caused a form submission...)

Edited by jwzumwalt
Link to comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...