How to call external javascript file in PHP?

前端 未结 4 1682
无人共我
无人共我 2020-12-17 05:05

I am learning how to call external javascript files in my PHP code. I got some codes from the internet and tried it but its not working. Can somebody pls give me some advice

相关标签:
4条回答
  • 2020-12-17 05:10

    You have not really specified what is not working but i am noticing something in your code.

    <html>
    <head>
     <script language="JavaScript" src="exer_1.js"></script>
    </head>
    <body>
     <form name="myform">
      <input type="text" id="input_1" name="input_1" /><br />
      <input type="text" id="input_2" name="input_2" /><br />
     <!-- The following will cause an error -->
      <input type="submit" value="Check!" onclick="javascript:parseTest() return false;" />
    
    <!-- instead use this -->
    <input type="submit" value="Check!" onclick="javascript:parseTest(); return false;" />
     </form>
    </body>
    </html>
    

    semicolon error

    0 讨论(0)
  • 2020-12-17 05:15

    I would look into JQuery for a nice javascript framework. Instead of putting javascript code on the button's "onclick" event, with jquery you could do something like:

    $('#submit').click(function() {
      // whatever code you want to run when submit is clicked.
      alert('Submit clicked');
    }
    
    0 讨论(0)
  • 2020-12-17 05:18

    <script language="JavaScript" src="exer_1.js"></script>

    The language attribute is deprecated, use type instead

    <script type="text/javascript" src="exer_1.js"></script>
    

    The correct syntax for inline event binding is

    <input type="submit" value="Check!" onclick="parseTest(); return false;" />
    

    You may want to consider moving the event handler to the form's submit event. Your function could then return false on error or true on success, which can then be used to determine whether the form submission continues or not, eg

    <form onsubmit="return parseTest()">
    
    0 讨论(0)
  • 2020-12-17 05:18
    <script type="text/javascript" src="exer_1.js"></script>
    
    0 讨论(0)
提交回复
热议问题