与PHP我想检查输入字段是否有效,而这样做,我把我的代码到不同的PHP文件,并将它们包含在主要的PHP文件(registerFunction.php) ,我这样做的原因是为了保持事件的组织性,只需点击一个按钮即可运行,但我得到的错误是我在registerFunction.php中声明的变量未在regValidation.php文件中声明。以下是错误消息我得到:未定义的变量:regErrors在PHP包含未声明的变量
我对registerFunction.php代码是:
<?php
//Create Error array
$regErrors = array();
//Add Date Functions
include "includes/functions/dateFunction.php";
//Checks if Register Screen Inputs are valid or not and pushes the errors into an array.
include "includes/functions/checkRegFields.php";
//
include "includes/functions/registryValidation.php";
if(regDecision()){
//register
}
else{
foreach ($regErrors as $errrorMessage) {
?>
<script>
$("#errors").append('<div class="alert alert-danger" role="alert"><?php echo $errrorMessage; ?></div>');
</script>
<?php
}
}
echo postDate();
?>
checkRegFields.php:
<?php
if(isset($_POST['registerButton'])){
if(($_POST['regEmail'])){
$regEmail = $_POST['regEmail'];
}
else{
array_push($regError,"Please Fill in the Email field");
}
if(($_POST['regUsername'])){
$regUsername = $_POST['regUsername'];
}
else{
array_push($regErrors,"Please Fill in the Username field");
}
if ((['regPassword']) and ($_POST['regPassword2'])) {
if(($_POST['regPassword']) == ($_POST['regPassword2'])){
$regPassword = $_POST['regPassword'];
}
else{
array_push($regErrors,"Passwords does not match!");
}
}
else{
array_push($regErrors,"Please Fill in the Password fields");
}
}
?>
最后我有registryValidation.php (我有一个错误):
<?php
function regDecision(){
if(sizeof($regErrors)==0){
return true;
}
else{
return false;
}
}
?>
checkRegFields.php罚款$ regErr或可变但 registryValidation.php告诉我,$ regError未声明。
这是为什么?
因为'regDecision'是一个函数,所以你需要'global $ regErrors'?另外请注意,您的电子邮件验证正在推向一个未定义的变量('regError'而不是'regErrors')。 – CompuChip
谢谢!现在你告诉我这些,它已经清楚了,我已经解决了我的问题=) – Licentia