所以,我试图验证7个数字的ID。但是,我的代码不断返回$ idErr而不是$ id本身。我不知道为什么,因为我已经验证了其他所有工作,并在几个验证器中检查了我的正则表达式。有任何想法吗? 注意:我输入的值是9876543,它在我使用的任何验证器中传递
请参阅//检查ID
<!DOCTYPE html>
<html>
<!--
Turnin page example
This page demonstrates a turnin form.
The user can type in their personal info and turn in a fake homework assignment.
-->
<head>
<title>Turnin Page</title>
</head>
<body>
<?php
// define variables and set to empty values
$studentname = $id = $assignment = $email = $cheat = " ";
$studentnameErr = $idErr = $assignmentErr = $emailErr = $cheatErr = " ";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//your code goes here.
function test($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// check name
if(empty($_POST["studentname"])) {
$studentnameErr = "You need a name!";
}elseif (!preg_match("/[a-z A-Z ]*$/",$studentname)) {
$studentnameErr = "Only letters and white space allowed";
}else{
$studentname = test($_POST["studentname"]);
}
// check id
if(empty($_POST["id"])) {
$idErr = "You need an id!";
} elseif(!preg_match("/([0-9]{7})/",$id)){
$idErr = "You need an id consisting of 7 numbers";
} else{
$id = test($_POST["id"]);
}
//check email
if(empty($_POST["email"])) {
$emailErr = "You need an email!";
}
elseif(!preg_match("/[([a-z]{2}\d{4}a{1}@american.edu{1})]/", $email)) {
$emailErr = "email needs to be: 2 consonants + 4 numbers + a@american.edu ";
} else{
$email = test($_POST["email"]);
}
//check assignment
// check if a file has been uploaded and if the file name is hw1, hw2,
if(empty($_POST["assignment"])) {
$assignmentErr = "You need to submit an assignment!";
} else{
$assignment = test($_POST["assignment"]);
}
// check cheating
if(empty($_POST["cheat"])) {
$cheatErr = "You need to say you won't cheat!";
} else{
$cheat = test($_POST["cheat"]);
}
// note you need to use $_FILES["fileToUpload"]["tmp_name"]
// to access your file.
}
?>
<h1>CSC 435 Turnin</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Personal Information</legend>
<div>
Name: <input type="text" name="studentname"/>
<span class="error">* <?php echo $studentnameErr;?></span>
</div>
<div>
Student id: <input type="text" name="id" maxlength="7"/>
<span class="error">* <?php echo $idErr;?></span>
</div>
<div>
Student email: <input type="text" name="email"/>
<span class="error">* <?php echo $emailErr;?></span>
</div>
</fieldset>
<fieldset>
<legend>Assignment Information</legend>
<div>
Assignment:
<label><input type="radio" name="assignment" value="1" /> 1</label>
<label><input type="radio" name="assignment" value="2" /> 2</label>
<label><input type="radio" name="assignment" value="3" /> 3</label>
<span class="error">* <?php echo $assignmentErr;?></span>
</div>
<div>
Code:
<input type="file" name="code" />
</div>
</fieldset>
<div>
<input type="checkbox" name="cheat" /> I promise I didn't cheat!
</div>
<input type="submit" />
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $studentname;
echo "<br>";
echo $id;
echo "<br>";
echo $email;
echo "<br>";
echo $assignment;
echo "<br>";
?>
</body>
</html>
答案 0 :(得分:3)
因为您正在检查$id
而未指定它。选中$_POST["id"]
或将$id
分配为$_POST["id"]
值。
这会告诉你:
error_reporting(E_ALL);
ini_set('display_errors', '1');
注意:未定义的变量:第X行的文件中的id
答案 1 :(得分:-1)
尝试/^[a-z A-Z ]*$/
而不是/[a-z A-Z ]*$/
,添加^