How to call external javascript file in PHP?

时光怂恿深爱的人放手 提交于 2019-11-27 18:54:28

问题


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


来源:https://stackoverflow.com/questions/5864228/how-to-call-external-javascript-file-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!