[英]PHP/jQuery AJAX JSON error
当我尝试使用jQuery Ajax从PHP页面接收代码时,我发现一个奇怪的错误:“未定义变量:错误”
<?php
$errors = array("already_signed" => "You are already signed in", "field_empty" => "All fields must be filled", "long_username" => "Username length must be less then 40 symbols", "incorrect_email" => "Your mail is incorrent", "user_exists" => "User with such username already exists", "account_not_found" => "Account not found", "passwords_arent_same" => "Passwords must be the same");
Function check_post() {
$answer = array("ok" => "false", "answer" => $errors["field_empty"]);
echo json_encode($answer);
}
check_post();
?>
如果我没有功能就回声-一切都会好的。 谢谢你的帮助
您似乎在其中至少缺少一个}
。 照原样,您的函数定义没有结束,因此它读为无限递归调用。
同样,您在函数之外定义了$errors
。 PHP不允许“较低”的代码作用域查看在较高作用域中定义的变量。 您需要在函数内将$ errors声明为全局变量:
<?php
$errors = array(....);
function check_post() {
global $errors;
$answer = ...
...
}
check_post();
您正在尝试从函数内部访问全局变量。 为了实现这一点,您需要使用“ global”关键字:
<?php
$errors = array("already_signed" => "You are already signed in", "field_empty" => "All fields must be filled", "long_username" => "Username length must be less then 40 symbols", "incorrect_email" => "Your mail is incorrent", "user_exists" => "User with such username already exists", "account_not_found" => "Account not found", "passwords_arent_same" => "Passwords must be the same");
function check_post() {
global $errors;
$answer = array("ok" => "false", "answer" => $errors["field_empty"]);
echo json_encode($answer);
}
check_post();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.