Fatal error: Call to undefined function
Fatal error: Call to undefined function
我正在尝试使用我在 head 标记中调用的 javascript 文件中定义的函数来验证输入到表单输入字段中的字符串,这是第一个包含在我的 index.php 文件。在调用 mod10() 函数之前我做了很多检查,这些检查都通过了,但是一旦调用 mod10() 函数,我就会收到以下错误:
Fatal error: Call to undefined function mod10() in
/Applications/XAMPP/xamppfiles/htdocs/modderfc/Content/new_registration.php
on line 46
这是抛出错误的代码块:
//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"]);
}
}
}
我尝试在 if (mod10($player_id_number,13) == FALSE) { line link this:
$player_id_number = test_input($_POST["player_id_number"]);
但这会导致相同的错误,这与之前失败的行相同:
致命错误:在第 47 行 /Applications/XAMPP/xamppfiles/htdocs/modderfc/Content/new_registration.php 中调用未定义的函数 mod10()
作为参考,我还在 .js 文件中包含了该函数:
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 & 1+0 = 1
case 12: calc = 3; break; //6*2=12 & 1+2 = 3
case 14: calc = 5; break; //7*2=14 & 1+4 = 5
case 16: calc = 7; break; //8*2=16 & 1+6 = 7
case 18: calc = 9; break; //9*2=18 & 1+8 = 9
default: calc = calc; //4*2= 8 & 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
}
您在这里将 php 与 javascript 混在一起。 Php 将永远无法执行您的 mod10
函数,因为它是用 JS 编写的。简单回答:在 php.
中重新编码您的 mod10
函数
了解客户端代码和服务器端代码之间的区别很重要。
PHP(服务器端)将首先 运行,然后生成 html(如果您愿意,还可以生成 JavaScript),然后浏览器将加载该过程的输出。
从那时起,您就在客户端,并拥有 JavaScript 等前端工具供您使用。
如果您想从 php 调用 JavaScript 函数,您可以在页面中回显 JavaScript 代码
<script>
部分,但该代码只会在浏览器加载页面后 运行 出现。 php 实际上不会 运行 该功能,只是将其打印为文本。
如果您希望从客户端访问服务器代码,您可以使用 AJAX。
我正在尝试使用我在 head 标记中调用的 javascript 文件中定义的函数来验证输入到表单输入字段中的字符串,这是第一个包含在我的 index.php 文件。在调用 mod10() 函数之前我做了很多检查,这些检查都通过了,但是一旦调用 mod10() 函数,我就会收到以下错误:
Fatal error: Call to undefined function mod10() in /Applications/XAMPP/xamppfiles/htdocs/modderfc/Content/new_registration.php on line 46
这是抛出错误的代码块:
//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"]);
}
}
}
我尝试在 if (mod10($player_id_number,13) == FALSE) { line link this:
$player_id_number = test_input($_POST["player_id_number"]);
但这会导致相同的错误,这与之前失败的行相同:
致命错误:在第 47 行 /Applications/XAMPP/xamppfiles/htdocs/modderfc/Content/new_registration.php 中调用未定义的函数 mod10()
作为参考,我还在 .js 文件中包含了该函数:
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 & 1+0 = 1
case 12: calc = 3; break; //6*2=12 & 1+2 = 3
case 14: calc = 5; break; //7*2=14 & 1+4 = 5
case 16: calc = 7; break; //8*2=16 & 1+6 = 7
case 18: calc = 9; break; //9*2=18 & 1+8 = 9
default: calc = calc; //4*2= 8 & 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
}
您在这里将 php 与 javascript 混在一起。 Php 将永远无法执行您的 mod10
函数,因为它是用 JS 编写的。简单回答:在 php.
mod10
函数
了解客户端代码和服务器端代码之间的区别很重要。 PHP(服务器端)将首先 运行,然后生成 html(如果您愿意,还可以生成 JavaScript),然后浏览器将加载该过程的输出。
从那时起,您就在客户端,并拥有 JavaScript 等前端工具供您使用。
如果您想从 php 调用 JavaScript 函数,您可以在页面中回显 JavaScript 代码
<script>
部分,但该代码只会在浏览器加载页面后 运行 出现。 php 实际上不会 运行 该功能,只是将其打印为文本。
如果您希望从客户端访问服务器代码,您可以使用 AJAX。