如何在PHP中调用外部javascript文件? [英] How to call external javascript file in PHP?

查看:91
本文介绍了如何在PHP中调用外部javascript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在PHP代码中调用外部javascript文件.我从互联网上获得了一些代码,并尝试了一下,但是没有用.有人可以给我一些建议或向我解释一下吗.我不是程序员,但是我正在研究如何编程,因此刚刚开始学习,这就是为什么我难以理解某些概念的原因.

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.

我这里有代码,PHP文件和JS文件.它们在同一文件夹中.

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

以下是代码:

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

推荐答案

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

不推荐使用language属性,请改用type

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;" />


您可能要考虑将事件处理程序移至表单的Submit事件.然后,您的函数可能会在错误时返回false,或者在成功时返回true,然后可以将其用于确定表单提交是否继续,例如


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

这篇关于如何在PHP中调用外部javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆