dtzk85937 2014-03-23 20:51
浏览 53
已采纳

尝试验证表单时,PHP不会显示HTML [关闭]

This is my first real attempt at coming to StackExchange and I wanted my question to be as relevant as possible. Basically, I am following along in the O'Reilly book to Learn PHP, MySQL, Javascript, etc. and I'm a bit stuck on Chapter 16's authentication protocol. Basically the author wants us to just put the CSS and JS in the echo <<<_END statements, I'm trying to put it so they can still be kept separate in their external file.

My problem is, when I load the PHP file, absolutely nothing comes up. Not even a title. I was concerned this might have something to do with the fact that I'm running WebMatrix and an IIS Express server for development, but I have been able to run other, much smaller echo <<<_END statements fine when I went to test them.

Below is the code, if anyone has input I would be very grateful.

<?php 

$firstname = $lastname = $username = $password = $age = $email = "";

if (isset($_POST['firstname']))
{
    $firstname = fix_string($_POST['firstname']);
}

if (isset($_POST['lastname']))
{
    $lastname = fix_string($_POST['lastname']);
}

if (isset($_POST['username']))
{
    $username = fix_string($_POST['username']);
}

if (isset($_POST['password']))
{
    $password = fix_string($_POST['password']);
}

if (isset($_POST['age']))
{
    $age = fix_string($_POST['age']);
}

if (isset($_POST['email']))
{
    $email = fix_string($_POST['email']);
}

$fail  = valide_firstname($firstname);
$fail .= valide_lastname($lastname);
$fail .= valide_username($username);
$fail .= valide_password($password);
$fail .= valide_age($age);
$fail .= valide_email($email); 

echo <<<_END
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
<title>An Example Form</title>    
<link rel="stylesheet" type="text/css" href="StyleSheet.css>
</head>
_END;

if ($fail = "")
{
    echo "<body>Form data succesffully validated: $firstname, $lastname, $username, $password, $age, $email.</body></html>";

    //database entry field

    exit;
}

echo <<<_END
<body>
<table class="signup" border="0" cellpadding="2"
cellspacing="5" bgcolor="#eeeeee">
<th colspan="2" align="center">Signup Form</th>

<tr><td colspan="2">Sorry, the following errors were found<br />
in your form: <p><font color=red size=1><i>$fail</i></font></p>
</td></tr>

<form method="post" action = "adduser.php"
onSubmit = "return validate(this)">
<tr><td>First name</td><td><input type ="text" maxlength = "32"
name = "firstname" value = "$firstname" /></td></tr>
<tr><td>Last name</td><td><input type ="text" maxlength = "32"
name = "lastname" value = "$lastname" /></td></tr>
<tr><td>Username</td><td><input type ="text" maxlength = "16"
name = "username" value = "$username" /></td></tr>
<tr><td>Password</td><td><input type ="text" maxlength = "12"
name = "password" value = "$password" /></td></tr>
<tr><td>Age</td><td><input type ="text" maxlength = "3"
name = "age" value = "$age" /></td></tr>
<tr><td>Email</td><td><input type ="text" maxlength = "64"
name = "email" value = "$email" /></td></tr>
<tr><td colspan = "2" align = "center"><input type = "submit" value = "Signup" /></td></tr></form></table>
<script type="text/javascript" src="validate.js"></script>
</body>
</html>
_END;

function validate_fistname($field){
    if ($field == "") return "No First Name was entered <br/>";
}

function validate_lastname($field){
    if ($field == "") return "No Last Name was entered <br/>";
}

function validate_username($field){
    if ($field == "") return "No Username was enterted <br/>";
    else if (strlen($field) < 5) return "Usernames must be at least 5 characters long <br/>";
    else if (preg_match("/[\W]/", $field)) return "Only letters, numbers, - and _ is usernames<br/>";
    return "";
}

function validate_password($field){
    if ($field =="") return "No Password was entered <br/>";
    else if (strlen($field) < 6) return "Passwords must be at least 6 characters in length <br/>";
    else if (!preg_match("/[a-z]/", $field) ||
             !preg_match("/[A-Z]/", $field) ||
             !preg_match("/[\d]/", $field))
             return "Passwords require one upper case, one lower case, and one number<br/>";

    return "";
}

function validate_age($field) {
    if ($field == "") return "No Age was entered<br />";
    else if ($field < 18 || $field > 110)
    return "Age must be between 18 and 110<br />";
    return "";
}

function validate_email($field) {
    if ($field == "") return "No Email was entered<br />";
    else if (!((strpos($field, ".") > 0) &&
        (strpos($field, "@") > 0)) ||
        preg_match("/[^a-zA-Z0-9.@_-]/", $field))
    return "The Email address is invalid<br />";
    return "";
}

function fix_string($string) {
    if (get_magic_quotes_gpc()) $string = stripcslashes ($string);
    return htmlentities ($string);
}
?>
  • 写回答

2条回答 默认 最新

  • douchui4459 2014-03-23 21:03
    关注

    You have syntax errors.
    Turn on error reporting as described here.

    Error 1
    You have spelt validate wrong. This -

    $fail  = valide_firstname($firstname);
    $fail .= valide_lastname($lastname);
    $fail .= valide_username($username);
    $fail .= valide_password($password);
    $fail .= valide_age($age);
    $fail .= valide_email($email); 
    

    must be -

    $fail  = validate_firstname($firstname);
    $fail .= validate_lastname($lastname);
    $fail .= validate_username($username);
    $fail .= validate_password($password);
    $fail .= validate_age($age);
    $fail .= validate_email($email); 
    


    Error 2
    Your validate_firstname() function definition is missing an 'r' -

    function validate_fistname($field){
                        ^ Needs an 'r' here.
    

    Error 3
    The condition if ($fail = "") should be if ($fail == "").

    You are presently doing an assignment with = instead of doing a comparison ==

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?