與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