0
爲用戶設置一些指導,讓用戶按照輸入進行操作,我相信我的語法和編碼是正確的,但也許我沒有正確引用第二個php文件?我是新來的PHP,所以我的第一個直覺是爲我的文本框使用onfocus或onblur函數,但這是錯誤的,所以任何幫助將非常有用。謝謝。PHP錯誤檢查問題
的index.php
<span class ="error"> * required field </span>
<form name ="myForm" action ="display.php<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"
method = "POST"><br>
Name: <input type="text" name="userName" value ="<?php echo $name;?>"
<span class ="error"> *<?php echo $nameErr;?></span><br>
University: <input type="text" name="userUniv" value ="<?php echo $university;?>"
<span class ="error"> *<?php echo $univErr;?></span><br>
Email: <input type ="text" name ="userEmail" value ="<?php echo $email;?>"
<span class ="error"> *<?php echo $emailErr;?></span><br><br>
Display.php的
<?php
$nameErr = $emailErr = $univErr = "";
$name = $email = $university = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["userName"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["userName"]);
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$nameErr = "Invalid name";
}
}
if (empty($_POST["userEmail"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["userEmail"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["userUniv"])) {
$univErr = "University is required";
} else {
$university = test_input($_POST["userUniv"]);
if (!preg_match("/^[a-zA-Z ]*$/", $university)) {
$univErr = "Invalid University";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
這是什麼'action =「display.php <?php echo htmlspecialchars($ _ SERVER ['PHP_SELF']);?>」'?它不應該是'second.php'嗎? – AbraCadaver
對不起,我的實際文件名是display.php – StevieP
你想讓你的錯誤變量從display.php出現在index.php的表單中嗎?在你的display.php中沒有任何跡象表明你在處理這些錯誤消息。如果是這樣的話,你需要index.php裏面的display.php代碼,然後用post或者表單提交。 – eomer