在提交表单时重定向到.php页面,否则显示警告


redirecting to a .php page on submit of form else display alert

用户输入用户名和密码后,点击提交按钮(go)..调用数据库类(在temp.php中)中的一个方法confirm()来检查用户名和密码是否匹配。如果匹配,'tm.php'必须加载,否则必须显示一个警告框,提示用户名和密码不匹配!警报框必须在第一。php,这是用户输入登录信息的页面。代码片段:

temp.php:

class database{
function confirm($uname,$pwd){
//include('tm.php');
$con = new mysqli("localhost","root","","hotels");
$res = $con->query("select * from user");
$row=$res->fetch_assoc();
$flag=0;
while($row)
{
if($row['username'] == $uname && $row['password'] == $pwd)
{$flag=1;
include('tm.php');
break;
}
$row=$res->fetch_assoc();
}
if($flag==1)?>
<script>alert("Username and Password do not match!");</script>
<?php
}
}
?>
}

first.php:

 <form action="first.php">
    Username: <input type="text" name="uname"><br>
    Password: <input type="text" name="pwd"><br>
    <input type="submit" name="go" value="GO">
    </form>
    <?php
    require('temp.php');
     if (isset($_POST['go']))
    {
    $db=new database();
    $uname=$_POST['uname'];
    $pwd=$_POST['pwd'];
    $db->confirm($uname,$pwd);
    }
    ?>

警告框没有出现,也没有被定向到tm.php.请帮我整理一下!

试试这个,

class database{
   function confirm($uname,$pwd){
   //include('tm.php');
   $con = new mysqli("localhost","root","","hotels");
   $res = $con->query("select * from user");
   $row=$res->fetch_assoc();
   $flag=0;
   while($row)
   {
       if($row['username'] == $uname && $row['password'] == $pwd)
       {
           $flag=1;
           include('tm.php');
           break;
       }
       $row=$res->fetch_assoc();
   }
   if($flag==1)
   {
       echo "<script>alert('Username and Password do not match!');</script>";
       header("refresh:0;url=first.php");
   }
  }
}
class database{
function confirm($uname,$pwd){
//include('tm.php');
$con = new mysqli("localhost","root","","hotels");
$res = $con->query("select * from user");
$row=$res->fetch_assoc();
$flag=0;
while($row)
{
   if($row['username'] == $uname && $row['password'] == $pwd)
   {
       $flag=1;
 // your include syntax was also wrong.
       include 'tm.php';
       break;
   }
   $row=$res->fetch_assoc();
}
// And here is your logical error. Changed it from 0 to 1. 
if($flag==0)
{
   echo "<script>alert('Username and Password do not match!');</script>";
   header("refresh:0;url=first.php");
  }
 }
}