Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call external javascript file in PHP?

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!!!");
  }
 } 
}
like image 996
Newbie Coder Avatar asked Dec 04 '22 22:12

Newbie Coder


1 Answers

<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()">
like image 178
Phil Avatar answered Dec 10 '22 09:12

Phil