回声显示在错误的地方

时间:2014-04-14 01:56:19

标签: php html css web

我正在为一个类创建一个带有PHP的页面,当我回显它出现在错误的地方时。

这是我的HTML页面

<html>
<head>
    <link rel="stylesheet" href="Site.css">
    <?php include("Header.php"); ?>
    </div>
</head>

<body>
    <div id="main">
        <h1>About</h1>

        <form action="Insert.php" method="post">
            <table>
                <tr>
                    <td><span>First name:</span></td>
                    <td><input type="text" name="firstname"></td>
                </tr>
                <tr>
                    <td><span>Last name:</span></td>
                    <td><input type="text" name="lastname"></td>
                </tr>
                <tr>
                    <td><span>Age:</span></td>
                    <td><input type="number" name="age"></td>
                </tr>
            </table>

            <input type="submit">
        </form>

        <?php include("Footer.php");?>
    </div> 
</body>
</html>

这是我的PHP页面:

<?php 
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if(!$con) {
    die("could not connect to localhost:" .mysql_error());
}

mysql_select_db("a7068104_world") or die("Cannot connect to database");

header("refresh:1.5; url=NamesAction.php");

$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$fullname = mysql_real_escape_string($_POST['firstname'] . " " . $_POST['lastname']);
$age = mysql_real_escape_string($_POST['age']);
$query = "SELECT * FROM names_1 WHERE fullname='$fullname'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ){
        echo "Your name is already in the database and will not be added again!";
}
else {
        $query = "INSERT INTO names_1 (firstname, lastname, fullname, age) VALUES('$firstname', '$lastname', '$fullname', '$age')";
        $result = mysql_query($query);
        if($result) {
            echo "Your name was successfully added to the database!";
        }
        else{
            echo "Your name couldn't be added to the database!";
        }
}

mysql_close($con);
?>

<html>
<head>
    <link rel="stylesheet" href="Site.css">
    <?php include("Header.php"); ?>
    </div>
</head>

<body>
    <div id="main">
        <h1>Names</h1>
        <p>You will be redirected back to the <b>Names</b> page in a moment.</p>
        <?php include("Footer.php");?>
    </div>
</body>
</html>

当我在PHP页面中回显东西时,它显示在框架的最顶部,它位于

的正上方
<div id="main">

我希望回显的文字放在

的最底部
<div id="main">

有什么方法可以做到吗?感谢您的帮助!

谢谢, Leonardude

1 个答案:

答案 0 :(得分:2)

您的问题是,在提供HTML之前,您回复消息。 这在这里很明显:

        if($result) {
            echo "Your name was successfully added to the database!";
        }
        else{
            echo "Your name couldn't be added to the database!";
        }

因为 PHP 是服务器端语言而HTML是客户端语言,所以PHP将在HTML之前处理好,这意味着它将在页面显示之前回显。因此问题出在<div id="main"></div>之前。

解决这个问题的方法是设置一个变量

        if($result) {
            $var = "Your name was successfully added to the database!";
        }
        else{
            $var = "Your name couldn't be added to the database!";
        }

在你<div id="main"></div>的某个地方,您可以执行以下操作:

<div id="main">

<?php 
    if(isset($var) && !empty($var)) {
        echo $var;
    }

?>

</div>