0

我写了这个“登录”页面,昨晚一切正常,除了没有显示错误消息。今天,我去仔细检查我的工作,我收到了这个错误消息:Call to undefined function showForm() in E:\wwwroot\teamc\logIn.php on line 7. 我没有从昨晚更改或更改我的代码今天早上。谁能看到为什么现在出现错误消息?

<?php
session_start();

    //validate text was entered in UserName text box
if(empty($_POST['txtUserName']))
    {
        showForm('Please Enter A User Name.');
        exit();
    }
else
    {
        $UserName = $_POST['txtUserName'];
    }

//validate text was entered in password text box
if(empty($_POST['txtPassword']))
    {
        showForm('Please Enter A Password.');
        exit();
    }
else
    {
        $Password = $_POST['txtPassword'];
    }

if($Password != Password($UserName))
    {
        showForm('User Name And Password Do Not Match!');
        exit();
    }


function Password($UserName)
{
//database login
$dsn = 'mysql:host=XXX;dbname=XXX';
$username='XXX';
$password='XXX';
//variable for errors
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
//try to run code
try {
//object to open database
$db = new PDO($dsn,$username,$password, $options);
//check username against password
    $SQL = $db->prepare("Select USER_PASSWORD FROM user WHERE user_name =  :UserName");
    $SQL->bindValue(':UserName', $UserName);
    $SQL->execute();
    $username = $SQL->fetch();

    if($username === false)
        {
            $password = null;
        }
    else
        {
            $password = $username['USER_PASSWORD'];
            include 'index.php';
        }

    return $password;
    $SQL->closeCursor();
    $db = null;

    } catch(PDOException $e){
        $error_message = $e->getMessage();
        echo("<p>Database Error: $error_message</p>");
        exit();
    }

}
function showForm($formMessage)
{?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1  /DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WisCon Log In</title>
<link rel="stylesheet" href="styles/wiscon-default-styles.css" type="text/css" />
<link rel="stylesheet" href="styles/FormStyle.css" type="text/css" />
<script type="text/javascript" src="js/validateLogInForm.js/validateLogInForm.js">  </script>
</head>

<body id="logPage">
<div id="wrapper">

    <?php include('includes/header.php'); ?>
    <?php include('includes/topNavigation.php'); ?>

    <div id="mainContent">

        <div class="formDiv">
            <form name="registerForm" id="registerForm" action="" method="post">
                <h1 style="color:#FF530D; text-align: center">Log into your account here!</h1>

                <fieldset id="security">
                    <legend>Security</legend>
                    <label for="txtUserName" class="boxLabel">User Name:</label>
                    <input type="text" id="txtUserName" name="txtUserName" autofocus="autofocus" required="required" />
                    <script type="text/javascript">
                        if(!("autofocus" in document.createElement("input")))
                        {
                            setTimeout(function(){
                                document.getElementById("txtUserName").focus();
                            }, 10);

                        }
                        </script>
                    <label for="txtPassword" class="boxLabel">Password:</label>
                    <input type="password" id="txtPassword" name="txtPassword" required="required" />
                </fieldset>

                <fieldset id="submission">
                    <div id="buttons">
                        <input type="submit" id="btnSubmit" name="btnSubmit" value="Submit" onclick="return validateLogInForm()"/>
                        <input type="reset" id="btnReset" name="btnReset" >
                    </div><!--end buttons-->
                </fieldset>
</p>
            </form>
        </div><!--end div class=formDiv-->
    </div><!--end div id=mainContent-->

    <?php include('includes/footer.php'); ?>

</div><!--end div id=wrapper-->
</body>
</html>
<?php
}
?>
4

2 回答 2

0

从手册exit()

输出一条消息并终止当前脚本

所以之后的任何事情,包括你定义函数的地方都不会被执行

没有必要exit()在 an中使用,if/else因为它不会elseif返回时执行true

编辑
如果需要,请将exit()if移到if 上方,以防止它不被执行/无法调用。if/elsefunction showForm($formMessage)

编辑 2
要使错误消息可见,请尝试添加类似这样的内容 -

<?php if($formMessage !="") echo "<h2 style=\"color:#FF0000; text-align: center\">".$formMessage."</h2>"; ?>

之间

...
<h1 style="color:#FF530D; text-align: center">Log into your account here!</h1>
<?php if($formMessage !="") echo "<h2 style=\"color:##FF0000; text-align: center\">".$formMessage."</h2>"; ?>
<fieldset id="security">
...
于 2013-03-16T22:03:41.220 回答
0

为什么exit()在调用函数后使用?尝试删除它,看看它是否有效。

另外,您没有在 html 代码中看到错误消息的原因仅仅是因为您没有$formMessage在 html 输出中的某处调用传递的变量。

于 2013-03-16T22:00:53.030 回答