gpt4 book ai didi

PHP/jQuery AJAX JSON 错误

转载 作者:行者123 更新时间:2023-11-28 12:07:56 25 4
gpt4 key购买 nike

当我尝试使用 jQuery Ajax 从 PHP 页面接收代码时,我发现了一个奇怪的错误:“ undefined variable :错误”

<?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();

关于PHP/jQuery AJAX JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7180720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com