我写的代码似乎有一个重大错误,使我的页面显示为空白。我是PHP的菜鸟,似乎我正在做的事情是不正确的。有人可以发现错误并可能解释我将来如何识别这个错误吗?
此外,在发表评论之前,我知道在这里使用GET是一件坏事,最终的代码将使用POST,但这是我调试的方式。
<?php
{
if(!preg_match( "^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*$" , $_GET['username']))
{
echo username correct
}
if($_GET['password'] == $_GET['passwordc'])
{
if(!preg_match( "^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*$" , $_GET['password']))
{
echo password correct;
}
}
if(!preg_match( "^[a-zA-Z]{1,}$" , $_GET['fName']))
{
echo First Name correct;
}
if(!preg_match( "^[a-zA-Z]{1,}$" , $_GET['lName']))
{
echo Last Name correct;
}
if(!preg_match( "^\d{1,2}$" , $_GET['age']))
{
echo age correct;
}
if(!preg_match( "^(m|M|f|F)$" , $_GET['gender']))
{
echo Gender correct;
}
if(!preg_match( "^(((0[13578]|(10|12))/(0[1-9]|[1-2][0-9]|3[0-1]))|(02/(0[1-9]|[1-2][0-9]))|((0[469]|11)/(0[1-9]|[1-2][0-9]|30)))/[0-9]{4}$" , $_GET['birthday']))
{
echo Birthday correct;
}
if(!preg_match( "^\d{4}$" , $_GET['year']))
{
echo Graduation year correct;
}
}
?>
<html>
<form method="GET" action="">
Enter Username: <input name="username" type="text" />
<br>
Enter Password: <input name="password" type="password" />
<br>
Confirm Password: <input name="passwordc" type="password" />
<br>
<br>
Personal Info:
<br>
Enter First Name: <input name="fName" type="text" />
<br>
Enter Last Name: <input name="lName" type="text" />
<br>
Enter Age:<input name="age" type="text" />
<br>
Enter Gender (m/f):<input name="gender" type="text" />
<br>
Enter Birthday (mm/dd/yyyy):<input name="birthday" type="text" />
<br>
Enter Graduation Year:<input name="year" type="text" />
<br>
<input type="submit" name="submit" value = "Submit!" />
</form>
</html>
答案 0 :(得分:1)
<?php
if(isset($_GET['submit'])) {
if(!preg_match( '/^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*$/' , $_GET['username']))
{
echo "username correct";
}
if($_GET['password'] == $_GET['passwordc'])
{
if(!preg_match( '/^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*$/' , $_GET['password']))
{
echo "password correct";
}
}
if(!preg_match( '/^[a-zA-Z]{1,}$/' , $_GET['fName']))
{
echo "First Name correct";
}
if(!preg_match( '/^[a-zA-Z]{1,}$/' , $_GET['lName']))
{
echo "Last Name correct";
}
if(!preg_match( '/^\d{1,2}$/' , $_GET['age']))
{
echo "age correct";
}
if(!preg_match( '/^(m|M|f|F)$/' , $_GET['gender']))
{
echo "Gender correct";
}
if(!preg_match( '/^(((0[13578]|(10|12))/(0[1-9]|[1-2][0-9]|3[0-1]))|(02/(0[1-9]|[1-2][0-9]))|((0[469]|11)/(0[1-9]|[1-2][0-9]|30)))/[0-9]{4}$/' , $_GET['birthday']))
{
echo "Birthday correct";
}
if(!preg_match( '/^\d{4}$/' , $_GET['year']))
{
echo "Graduation year correct";
}
}
?>
<html>
<form method="GET" action="">
Enter Username: <input name="username" type="text" />
<br>
Enter Password: <input name="password" type="password" />
<br>
Confirm Password: <input name="passwordc" type="password" />
<br>
<br>
Personal Info:
<br>
Enter First Name: <input name="fName" type="text" />
<br>
Enter Last Name: <input name="lName" type="text" />
<br>
Enter Age:<input name="age" type="text" />
<br>
Enter Gender (m/f):<input name="gender" type="text" />
<br>
Enter Birthday (mm/dd/yyyy):<input name="birthday" type="text" />
<br>
Enter Graduation Year:<input name="year" type="text" />
<br>
<input type="submit" name="submit" value = "Submit!" />
</form>
</html>
我还添加了提交检查。表单加载,我没有测试超过那个。