How to call external javascript file in PHP?

2019-01-24 18:04发布

问题:

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 or explain to me this. I am not a programmer but I am studying how to program and just started learning that's why I have difficulty understanding some concepts.

I have here the codes, PHP File and JS file. They are in the same folder.

Here are the codes:

index.php

<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 />
  <input type="submit" value="Check!" onclick="javascript:parseTest() return false;" />
 </form>
</body>
</html>

exer_1.js

function parseTest() {
 var elem_1 = document.getElementById('input_1');
 var elem_2 = document.getElementById('input_2');

 var inp_1 = elem_1.value;
 var inp_2 = elem_2.value;

 if (inp_1 == "" && inp_2 == "") {
  alert("You need to enter integers!!!");
  elem_1.focus();
 } else if (inp_1 == ""){
  alert("You need to enter Integer 1!!!");
  elem_1.focus();
 } else if (inp_2 == ""){
  alert("You need to enter Integer 2!!!");
  elem_2.focus();;
 } else {
  if (!parseInt(inp_1) || !parseInt(inp_2)) alert ("Enter Integers only!!!");
  else {
   alert("Correct Inputs!!!");
  }
 } 
}

回答1:

<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()">


回答2:

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



回答3:

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

Phil: Hope you don't mind my edit



回答4:

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');
}