简体   繁体   中英

Fatal error: Call to undefined function

I'm trying to validate a string that is entered into an input field on a form using a function defined in a javascript file that I am calling in the head tags, which is the first include in my index.php file. There are a number of checks I am doing before calling the mod10() function which are all passing, but as soon as the mod10() function is called I get the following error:

Fatal error: Call to undefined function mod10() in /Applications/XAMPP/xamppfiles/htdocs/modderfc/Content/new_registration.php on line 46

Here is the block of code where the error is being thrown:

//Check if the ID Number entered is a valid ID Number
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $fieldTitle = "Player ID Number";
    if (empty($_POST["player_id_number"])) {
        $player_id_numberErr = $fieldTitle . $requiredErrorText;
    } else {
        // check if the ID Number entered is a valid ID Number
        if (mod10($player_id_number,13) == FALSE) {           //line 46
            $player_id_numberErr = $id_numberValidationErrorText;
        } else {
            $player_id_number = test_input($_POST["player_id_number"]);
        }
    }
}

I tried setting the $player_id_number field as well using the following before the if (mod10($player_id_number,13) == FALSE) { line link this:

$player_id_number = test_input($_POST["player_id_number"]);

but that results in the same error, which is the same line it is failing as before:

Fatal error: Call to undefined function mod10() in /Applications/XAMPP/xamppfiles/htdocs/modderfc/Content/new_registration.php on line 47

For reference I have also included the function in the .js file:

function mod10(num, validLength) {  
if (num == "" || num == null) return true;
var valid = "0123456789"; 
var sCCN = num.toString().replace (/ /g,''); 
var len = sCCN.length;  
var iCCN = parseInt(sCCN,10);
var iTotal = 0;  // integer total set at zero
var bResult = false;  
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

for (var j=0; j<len; j++) {
    temp = "" + sCCN.substring(j, j+1);
    if (valid.indexOf(temp) == "-1"){bResult = false;}
}

if(len != validLength) {
      bResult = false;
}
else
{  // num is proper length - let's see if it is a valid mod10 number

    for (var i=len;i>0;i--) {               // LOOP throught the digits 
          calc = parseInt(iCCN,10) % 10;    // right most digit
          calc = parseInt(calc,10);         // assure it is an integer
          iTotal += calc;                   // running total of the number as we loop - Do Nothing to first digit
          i--;                              // decrement the count - move to the next digit
          iCCN = iCCN / 10;                 // subtracts right most digit 
          calc = parseInt(iCCN,10) % 10 ;   // NEXT right most digit
          calc = calc *2;                   // multiply the digit by two

          switch(calc){
            case 10: calc = 1; break;       //5*2=10 &amp; 1+0 = 1
            case 12: calc = 3; break;       //6*2=12 &amp; 1+2 = 3
            case 14: calc = 5; break;       //7*2=14 &amp; 1+4 = 5
            case 16: calc = 7; break;       //8*2=16 &amp; 1+6 = 7
            case 18: calc = 9; break;       //9*2=18 &amp; 1+8 = 9
            default: calc = calc;           //4*2= 8 &amp;   8 = 8  -same for all lower numbers
          }                 

        iCCN = iCCN / 10;  // subtracts right most digit 
        iTotal += calc;  // running total of the number as we loop

      }  // END OF LOOP

    bResult =  iTotal%10 == 0 // check to see if the sum Mod 10 is zero

    }
return bResult; // Return the results

}

It is important to understand the difference between client side code and server side code. the PHP (server side) will run first, and generate html (and JavaScript if you wish), and the browser will load the output of that process.

from that point you are in the client side and have front end tools such as JavaScript at your disposal.

if you wish to call a JavaScript function from php you can echo the JavaScript code in the page in a <script> section, but that code will only run after the browser loads the page. the php will not actually run that function, just print it as text.

if you wish to to access the server code from the client side you can use AJAX.

You are mixing php with javascript here. Php will never be able to execute your mod10 function because it's written in JS. Plain answer: Recode your mod10 function in php.

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