简体   繁体   English

如何在PHP中调用外部javascript文件?

[英]How to call external javascript file in PHP?

I am learning how to call external javascript files in my PHP code.我正在学习如何在我的 PHP 代码中调用外部 javascript 文件。 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.我这里有代码,PHP 文件和 JS 文件。 They are in the same folder.它们在同一个文件夹中。

Here are the codes:以下是代码:

index.php 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 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!!!");
  }
 } 
}

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

The language attribute is deprecated, use type instead不推荐使用language属性,请改用type

<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然后,您的 function 可能会在错误时返回false或在成功时返回true ,然后可用于确定表单提交是否继续,例如

<form onsubmit="return parseTest()">
<script type="text/javascript" src="exer_1.js"></script>

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分号错误

I would look into JQuery for a nice javascript framework.我会研究 JQuery 以获得一个不错的 javascript 框架。 Instead of putting javascript code on the button's "onclick" event, with jquery you could do something like:而不是将 javascript 代码放在按钮的“onclick”事件上,使用 jquery 您可以执行以下操作:

$('#submit').click(function() {
  // whatever code you want to run when submit is clicked.
  alert('Submit clicked');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM