我正在使用 .html 和 .php。在 default.html 中,用户必须输入一些信息,当单击按钮时,html 会发布到 default2.php。在 default2.php 中检查数据,如果正确,它将用户重定向到另一个页面。我遇到的问题是输入的数据错误。我在这里有两个问题:
- 当数据错误时,我会将用户重定向到 default.html,因为如果我不这样做,它将保留在 default2.php 中,而 default2.php 对用户来说没有什么重要的东西可以看到。我不知道这是否是最好的方法。
- 当输入的数据错误时,我希望在 default.html 中向用户发送回显消息。但我不知道如何从 default2.php 触发它。
我该如何解决这两个问题?谢谢...
默认.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP4</title>
<style type="text/css">
body {
    background-color: #CCC;
}
</style>
</head>
<body>
<p> </p>
<form id="form1" name="form1" method="post" action="default2.php">
  <p>
    <label for="textfield1">User Name</label>
    <input type="text" name="username" id="username" />
  </p>
  <p>
    <label for="textfield2">Password</label>
    <input type="password" name="password" id="password" />
  </p>
  <p>
    <input type="submit" name="button1" id="button1" value="Submit"  />
    <br />
    <br />
  <label id="label1">
  </label></p>
  <p> </p>  
</form>
<p> </p>
</body>
</html>
默认2.php:
<?php
require 'connection.php';
  if (isset($_POST['button1'])) {
    $username_v = $_POST['username'];
    $password_v = $_POST['password'];
    // Then you can prepare a statement and execute it.    
    $stmt = $dbh->prepare("CALL login(?, ?)");
    $stmt->bindParam(1, $username_v, PDO::PARAM_STR); 
    $stmt->bindParam(2, $password_v, PDO::PARAM_STR); 
    // call the stored procedure
    $stmt->execute();
    if ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) 
    {
          header("Location: main.php");
    }
    else
    {
        header("Location: default.html");
    }
  }
?>