When I was trying to recieve a code from PHP page using jQuery Ajax, I found a strange error: "Undefined variable: errors"
<?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();
?>
If I'll echo without function - everything will be ok. Thanks for any help
You seem to be missing at least one }
in there. As is, your function definition has no close, so it reads as an infinitely recursive call.
As well, you've got $errors
being defined OUTSIDE of your function. PHP does not allow "lower" code scopes to see variables defined in higher scopes. You need to declare $errors as a global within the function:
<?php
$errors = array(....);
function check_post() {
global $errors;
$answer = ...
...
}
check_post();
You are trying to access a global variable from within the function. In order to acomplish that, you need to use "global" keyword:
<?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();
?>