jwzumwalt Posted August 22, 2014 Share Posted August 22, 2014 (edited) 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 August 22, 2014 by jwzumwalt Link to comment Share on other sites More sharing options...
davej Posted August 22, 2014 Share Posted August 22, 2014 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 More sharing options...
dsonesuk Posted August 23, 2014 Share Posted August 23, 2014 (edited) Change the function name so it does not match id or name attribute values for example id="bankdate' or name='bankdate', its a conflict. Edited August 23, 2014 by dsonesuk 1 Link to comment Share on other sites More sharing options...
jwzumwalt Posted August 23, 2014 Author Share Posted August 23, 2014 (edited) 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 August 23, 2014 by jwzumwalt Link to comment Share on other sites More sharing options...
dsonesuk Posted August 23, 2014 Share Posted August 23, 2014 (edited) 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 August 23, 2014 by dsonesuk Link to comment Share on other sites More sharing options...
davej Posted August 24, 2014 Share Posted August 24, 2014 It is very common to set name and id to be identical inside a form. I wonder if that could ever cause a problem? Link to comment Share on other sites More sharing options...
dsonesuk Posted August 24, 2014 Share Posted August 24, 2014 Only if you use multiple identical id references for multiple radio buttons and checkbox form elements, which in most cases use the same name. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now