This is a very strange problem.
I have a webpage that accepts name/address information. I want to do a cross-check between the entered state and the entered zip code. I found a JS script that will return the state from the zip code right here on this site: How can I quickly determine the State for a given zipcode? I won’t copy it because it’s very long. There are no reported problems with this code.
Because it’s JS, I copy/pasted that code into a file called zipstate.js and added the following line in my script section (which is at the end of the html page)
<script src=”zipstate.js”></script>
I call that function here:
//cross check the zip to the state in case of typos
$zip1 = strval($zip); //make sure it's a string – could use (string) but same results
$state1 = getState($zip1); //this is where the error occurs
if ($state != $state1 ) { //never get here because of the error – and I don’t care about strict matching
$error_message .= 'The ZIP code does not belong to the entered state.<br/>';
}
When hit that section of code, I get an error: Fatal error: Uncaught Error: Call to undefined function getState() in [path] Stack trace: #0 {main} thrown in [path] on line 256
which is where that function call is.
I made sure the name of the function matches what I’m calling. I moved that script definition around, putting it in the section, putting it near the top of the , and actually imbedded the code into the main file, defining it as:
<script>
[copied code]
</script>
That didn't work. I took it out of the script and put it inline as php. Nope – that threw other errors.
I compared how this file was formatted versus working JS scripts, and I don't see any obvious flaws.
I have other JS and PHP functions defined that work as advertised.
Curiously, when I open up the developer’s panel, I see no errors under console, and under sources I see the file, and scanning the source, I don’t see any errors. Odd.
I’ve searched through this site for similar errors, but they all seem to relate to depreciated functions, or using MYSQL calls instead of MYSQLi. The script uses the keyword "typeof", which I don't see as being depreciated or removed.
So, Friends, what am I doing wrong? What have I missed?
This has been a fun, if time consuming, side project, and many of the problems I’ve encountered have been solved by poking around in the community. I’ve learned a lot. This time, I’m out of my league, and while this probably has a very simple solution, it’s beyond my ken. I’ve spent too much time on this, and I think I can use some help.
Thanks for the replies.