简体   繁体   中英

define a external javascript function inside php

I am trying to create a calculator using checkboxes, below is my code that works perfectly fine on html, but when this same code is in my php page I get a Uncaught ReferenceError: priceCalc() is not defined at HTMLInputElement.oninput when the <script src="assets/scripts/myscript.js"></script> is used in the php page it doesn't seem to work, only when I put the whole priceCalc(); function inside my head tags, then it works, but I don't want to do that, I want the javascript to be on its own externally called myscript.js...

 function priceCalc() { var totalCost = 0; //If referral is checked apply discount if (document.getElementById("opt1").checked) { totalCost += 3000; } //prints the final price document.getElementById("totalPrice").innerHTML = "$" + totalCost.toFixed(2); }
 <form method="post" oninput="priceCalc();"> <div class="item-option"> <label class="label"> <input type="checkbox" name="checkbox" value="text" id="opt1"> Uranium $3000 </label> </div> <div class="price"> <h3>Total estimated price: </h3> <p id="totalPrice">$0.00</p> </div> </form>

As mentioned, it's hard to say without your PHP code. But try the following:

  1. Ensure your <script> tag(s) are in the <head></head> section
  2. Double check your relative path in <script src="assets/scripts/myscript.js"></script> .

Testing in plnkr.co worked fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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