PHP Echo有关移动的帮助

时间:2014-09-07 02:40:14

标签: php html

您好我已经编写了一个PHP和HTML文件,我想将回显从我的Web浏览器顶部显示到html文件中的一个div,有关如何做到的任何想法?代码在下面,香港专业教育学院到处寻找,但一无所获。

<?php
session_start();
if(isset($_POST['submit'])){
    $user = $_POST['username'];
    $pwrd = $_POST['pwrd'];
    include ('../includes/db_connect.php');
    if(empty($user) || empty($pwrd)){
        echo 'Please enter a Username and Password!';
    }else{
        $user = strip_tags($user);
        $user = $db->real_escape_string($user); 
        $pwrd = strip_tags($pwrd);
        $pwrd = $db->real_escape_string($pwrd);
        $pwrd = md5($pwrd);
        $query = $db->query("SELECT user_id, username FROM user WHERE username='$user' AND password='$pwrd'");
        if($query->num_rows ===1){
            while($row = $query->fetch_object()){
                $_SESSION['user_id'] = $row->user_id;
            }
            header('Location: index.php');
            exit();
        }else{
            echo 'Wrong Username and Password!';
        }


    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<script scr="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<?php include '../includes/header.php' ?>
<div id="container">
    <form action="login.php" method="post">
        <p>
        <label>Username</label><input type="text" name="username" />
        </p>
        <p>
        <label>Password</label><input type="password" name="pwrd" />
        </p>
        <input type="submit" name="submit" value="Login" />
    </form>
</div>
<?php include '../includes/footer.php' ?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

你必须定义一个变量

你的规则

echo 'Please enter a Username and Password!';

必须是

$response = 'Please enter a Username and Password!';

你可以将它放入像这样的HTML标签

<div><?php echo $response; ?></div>

答案 1 :(得分:0)

<?php
session_start();
if(isset($_POST['submit'])){
    $user = $_POST['username'];
    $pwrd = $_POST['pwrd'];
    include ('../includes/db_connect.php');
    if (empty($user) || empty($pwrd)){
        $msg = 'Please enter a Username and Password!';
    } else{
        $user = strip_tags($user);
        $user = $db->real_escape_string($user); 
        $pwrd = strip_tags($pwrd);
        $pwrd = $db->real_escape_string($pwrd);
        $pwrd = md5($pwrd);
        $query = $db->query("SELECT user_id, username FROM user WHERE username='$user' AND password='$pwrd'");
        if($query->num_rows ===1){
            while($row = $query->fetch_object()){
                $_SESSION['user_id'] = $row->user_id;
            }
            header('Location: index.php');
            exit();
        } else{
            $msg = 'Wrong Username and Password!';
        }


    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
</head>
<body>
<?php include '../includes/header.php' ?>
<div id="container">
    <?php
    if ($msg) {
        echo $msg;
    }
    ?>
    <form action="login.php" method="post">
        <p>
        <label>Username</label><input type="text" name="username" />
        </p>
        <p>
        <label>Password</label><input type="password" name="pwrd" />
        </p>
        <input type="submit" name="submit" value="Login" />
    </form>
</div>
<?php include '../includes/footer.php' ?>
</body>
</html>