-1

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.

  • 2
    `` Try to avoid using curly quotes in programming, they break many things. – CertainPerformance Mar 11 '19 at 01:40
  • Are you trying to call the function from PHP? Because calling a JS function from PHP won't work. –  Mar 11 '19 at 01:46
  • @CertainPerformance - The curly quotes are an artifact from the word processing program I used to create the OP. I use HTML-KIT to write webcode, so there's really no curly quotes there. – F.M. Longo Mar 11 '19 at 09:26
  • @Chipster - I think you hit the nail on the head! Yes, that's exactly what I'm doing. This is the problem of learning on-the-fly instead of taking a course (and, at my very advanced age, there's really no point in doing that). This routine is easy enough to convert to a PHP function - use "is_string" instead of "typeof" and make all internal variable conform to PHP requirements. Many thanks! – F.M. Longo Mar 11 '19 at 09:26
  • You should make sure that code you post *exactly represents* the code you actually have, otherwise you may well get less-than-useful feedback. – CertainPerformance Mar 11 '19 at 09:27

1 Answers1

0

That would be because you are trying to call a Javascript function from PHP. You can't do that. The two are separate and independent from each other.

Generally, PHP can effect Javascript like so:

var somevar = <?php echo $myvar; ?>;

Note: This might be considered non-standard. This is just for an example.

But you cannot call a Javascript function from PHP. The two are independent from each other.

Solution: The solution is simple, port the Javascript function to PHP. This is often easier said than done, but this one doesn't look too hard to do so:

<?php // because we are in PHP, of course
function getState($zipcode) {

    // Ensure param is a string to prevent unpredictable parsing results
    if (is_string($zipcode)) {
        // It really depends on how you wish to log this. You could echo, or
        // return something, throw an exception even. For now I'v just commented it out
        // console.log('Must pass the zipcode as a string.');
        return;
    }

    // Ensure you don't parse codes that start with 0 as octal values
    $thiszip = (int)$zipcode; 

    // Code blocks alphabetized by state
    if ($thiszip >= 35000 && $thiszip <= 36999) {
        $thisst = 'AL';
        $thisstate = "Alabama";
        }
    else if ($thiszip >= 99500 && $thiszip <= 99999) {
        $thisst = 'AK';
        $thisstate = "Alaska";
        }
    else if ($thiszip >= 85000 && $thiszip <= 86999) {
        $thisst = 'AZ';
        $thisstate = "Arizona";
        }
    else if ($thiszip >= 71600 && $thiszip <= 72999) {
        $thisst = 'AR';
        $thisstate = "Arkansas";
        }
    else if ($thiszip >= 90000 && $thiszip <= 96699) {
        $thisst = 'CA';
        $thisstate = "California";
        }
    else if ($thiszip >= 80000 && $thiszip <= 81999) {
        $thisst = 'CO';
        $thisstate = "Colorado";
        }
    else if ($thiszip >= 6000 && $thiszip <= 6999) {
        $thisst = 'CT';
        $thisstate = "Connecticut";
        }
    else if ($thiszip >= 19700 && $thiszip <= 19999) {
        $thisst = 'DE';
        $thisstate = "Deleware";
        }
    else if ($thiszip >= 32000 && $thiszip <= 34999) {
        $thisst = 'FL';
        $thisstate = "Florida";
        }
    else if ($thiszip >= 30000 && $thiszip <= 31999) {
        $thisst = 'GA';
        $thisstate = "Georgia";
        }
    else if ($thiszip >= 96700 && $thiszip <= 96999) {
        $thisst = 'HI';
        $thisstate = "Hawaii";
        }
    else if ($thiszip >= 83200 && $thiszip <= 83999) {
        $thisst = 'ID';
        $thisstate = "Idaho";
        }
    else if ($thiszip >= 60000 && $thiszip <= 62999) {
        $thisst = 'IL';
        $thisstate = "Illinois";
        }
    else if ($thiszip >= 46000 && $thiszip <= 47999) {
        $thisst = 'IN';
        $thisstate = "Indiana";
        }
    else if ($thiszip >= 50000 && $thiszip <= 52999) {
        $thisst = 'IA';
        $thisstate = "Iowa";
        }
    else if ($thiszip >= 66000 && $thiszip <= 67999) {
        $thisst = 'KS';
        $thisstate = "Kansas";
        }
    else if ($thiszip >= 40000 && $thiszip <= 42999) {
        $thisst = 'KY';
        $thisstate = "Kentucky";
        }
    else if ($thiszip >= 70000 && $thiszip <= 71599) {
        $thisst = 'LA';
        $thisstate = "Louisiana";
        }
    else if ($thiszip >= 3900 && $thiszip <= 4999) {
        $thisst = 'ME';
        $thisstate = "Maine";
        }
    else if ($thiszip >= 20600 && $thiszip <= 21999) {
        $thisst = 'MD';
        $thisstate = "Maryland";
        }
    else if ($thiszip >= 1000 && $thiszip <= 2799) {
        $thisst = 'MA';
        $thisstate = "Massachusetts";
        }
    else if ($thiszip >= 48000 && $thiszip <= 49999) {
        $thisst = 'MI';
        $thisstate = "Michigan";
        }
    else if ($thiszip >= 55000 && $thiszip <= 56999) {
        $thisst = 'MN';
        $thisstate = "Minnesota";
        }
    else if ($thiszip >= 38600 && $thiszip <= 39999) {
        $thisst = 'MS';
        $thisstate = "Mississippi";
        }
    else if ($thiszip >= 63000 && $thiszip <= 65999) {
        $thisst = 'MO';
        $thisstate = "Missouri";
        }
    else if ($thiszip >= 59000 && $thiszip <= 59999) {
        $thisst = 'MT';
        $thisstate = "Montana";
        }
    else if ($thiszip >= 27000 && $thiszip <= 28999) {
        $thisst = 'NC';
        $thisstate = "North Carolina";
        }
    else if ($thiszip >= 58000 && $thiszip <= 58999) {
        $thisst = 'ND';
        $thisstate = "North Dakota";
        }
    else if ($thiszip >= 68000 && $thiszip <= 69999) {
        $thisst = 'NE';
        $thisstate = "Nebraska";
        }
    else if ($thiszip >= 88900 && $thiszip <= 89999) {
        $thisst = 'NV';
        $thisstate = "Nevada";
        }
    else if ($thiszip >= 3000 && $thiszip <= 3899) {
        $thisst = 'NH';
        $thisstate = "New Hampshire";
        }
    else if ($thiszip >= 7000 && $thiszip <= 8999) {
        $thisst = 'NJ';
        $thisstate = "New Jersey";
        }
    else if ($thiszip >= 87000 && $thiszip <= 88499) {
        $thisst = 'NM';
        $thisstate = "New Mexico";
        }
    else if ($thiszip >= 10000 && $thiszip <= 14999) {
        $thisst = 'NY';
        $thisstate = "New York";
        }
    else if ($thiszip >= 43000 && $thiszip <= 45999) {
        $thisst = 'OH';
        $thisstate = "Ohio";
        }
    else if ($thiszip >= 73000 && $thiszip <= 74999) {
        $thisst = 'OK';
        $thisstate = "Oklahoma";
        }
    else if ($thiszip >= 97000 && $thiszip <= 97999) {
        $thisst = 'OR';
        $thisstate = "Oregon";
        }
    else if ($thiszip >= 15000 && $thiszip <= 19699) {
        $thisst = 'PA';
        $thisstate = "Pennsylvania";
        }
    else if ($thiszip >= 300 && $thiszip <= 999) {
        $thisst = 'PR';
        $thisstate = "Puerto Rico";
        }
    else if ($thiszip >= 2800 && $thiszip <= 2999) {
        $thisst = 'RI';
        $thisstate = "Rhode Island";
        }
    else if ($thiszip >= 29000 && $thiszip <= 29999) {
        $thisst = 'SC';
        $thisstate = "South Carolina";
        }
    else if ($thiszip >= 57000 && $thiszip <= 57999) {
        $thisst = 'SD';
        $thisstate = "South Dakota";
        }
    else if ($thiszip >= 37000 && $thiszip <= 38599) {
        $thisst = 'TN';
        $thisstate = "Tennessee";
        }
    else if ( ($thiszip >= 75000 && $thiszip <= 79999) || ($thiszip >= 88500 && $thiszip <= 88599) ) {
        $thisst = 'TX';
        $thisstate = "Texas";
        }
    else if ($thiszip >= 84000 && $thiszip <= 84999) {
        $thisst = 'UT';
        $thisstate = "Utah";
        }
    else if ($thiszip >= 5000 && $thiszip <= 5999) {
        $thisst = 'VT';
        $thisstate = "Vermont";
        }
    else if ($thiszip >= 22000 && $thiszip <= 24699) {
        $thisst = 'VA';
        $thisstate = "Virgina";
        }
    else if ($thiszip >= 20000 && $thiszip <= 20599) {
        $thisst = 'DC';
        $thisstate = "Washington DC";
        }
    else if ($thiszip >= 98000 && $thiszip <= 99499) {
        $thisst = 'WA';
        $thisstate = "Washington";
        }
    else if ($thiszip >= 24700 && $thiszip <= 26999) {
        $thisst = 'WV';
        $thisstate = "West Virginia";
        }
    else if ($thiszip >= 53000 && $thiszip <= 54999) {
        $thisst = 'WI';
        $thisstate = "Wisconsin";
        }
    else if ($thiszip >= 82000 && $thiszip <= 83199) {
        $thisst = 'WY';
        $thisstate = "Wyoming";
        }
    else {
        $thisst = 'none';
    }
   return $thisst;
}
?>

And that should fix your problem.

  • That's exactly the way I modified the routine. Once I realized the error of my ways (thanks to your insight) it was less than five minutes to get the routine running. I really appreciate that you answered without any snark. – F.M. Longo Mar 12 '19 at 15:39