最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to call external javascript file in PHP? - Stack Overflow

programmeradmin1浏览0评论

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

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!!!");
  }
 } 
}
Share Improve this question asked May 3, 2011 at 1:39 Newbie CoderNewbie Coder 10.9k19 gold badges42 silver badges53 bronze badges 3
  • 1 What specifically is not working? – Trufa Commented May 3, 2011 at 1:42
  • 1 as you said you were new to all this, have you ever heard of Firebug plugin for firefox? Its seriously a developers best friend! (If you are using Chrome, it has a similar tool) – neolaser Commented May 3, 2011 at 1:45
  • I just forgot the semicolon... – Newbie Coder Commented May 3, 2011 at 1:55
Add a comment  | 

4 Answers 4

Reset to default 13

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

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. 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');
}
<script type="text/javascript" src="exer_1.js"></script>
发布评论

评论列表(0)

  1. 暂无评论