Jump to content

Calling external javascript function from within HTML generated using PHP


Recommended Posts

Hello,

 

I have a <select> statement which calls a javascipt function, updCities(), upon 'onchange'.  My HTML page is generated using php and the javascript is contained within <script> tags at the end of my php script.  The function takes the selected value from the <select> statement and populates the <options> of other <select> statements on the same page.

 

The function works fine as long as the javascript script is placed with <script> tags at the end of my php script.  But, if I place the javascript script in an external file (.js) I get an 'object expected' error at the line containing the 'onchange' call function.

 

I am not familiar with javascript.  I did not generate the code below and need help understanding how to call an external function.

 

How can I call an external javascript function?  Does the name of the external file have to be the name of the function?  How can I pass the value from the select statement to the external function?  Does the <script> tags have to be within the <head> tags??

 

Thanks, and here's my script...

 

//php document generating the HTML page

...

<select name="county" id="county" size=1 onchange="updCities(); ">

...

<script language="JavaScript" type="text/javascript" scr="www.arb.ca.gov/msprog/bus/rptg/js/droplists.js"></script>

 

 

// droplists.js JavaScript Document

var url = "getcities.php?county="; // passes value to php script that populates other select options

...

function updCities() {

 

  if (!isWorking && http) {

      var county = document.getElementById('county').value;

      http.open("GET", url + escape(county), true);

      http.onreadystatechange = handleHttpResponse;

      isWorking = true;

      http.send(null);

  }

}

 

By including the js file, it is as if it is part of the same file, nothing extra or special needs to be done. I generally reference the file locally, e.g. <script language="JavaScript" type="text/javascript" scr="droplists.js"></script>

 

 

n.b. this isn't the js forum

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.