如何在使用PHP提交表单后返回页面

时间:2017-02-08 09:58:38

标签: php html

我是PHP的新手,我只是在做一个简单的输入表单网页。问题是,在提交网站进入空白页面后,我希望它回到原来的那个。我在这里读到,可以通过向我的代码添加header()来帮助它,但它没有帮助。这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title> Sample Form </title>
    <link href = "stl.css" rel = "stylesheet" type="text/css" />
</head>
<body>

<form method="post" action="connect.php">
    <input class = "PaganForm" id = "name" type="text" name="name" value=""> 
    <input class = "PaganForm" id = "img" type="text" name="img" value=""> 
    <input class = "PaganForm" id = "dsc" type="text" name="dsc" value="">
    <input type="submit" id = "SubmitButton" > 
</form>
<?php
    header("Location: http://localhost/simple-form.php");
    exit;
    $servername = 'localhost';
    $username = 'root';
    $password = '';
    $database = 'cero_db';
    $conn = new mysqli($servername, $username, $password,$database);

    $sql = "SELECT * FROM pg";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        $newName = $row['Name'];
        $newLink = $row['Link'];
        $newDsc = $row['Description'];

        echo "<h1>".$newName."</h1>";
        echo "<img src = \"".$newLink."\" />";
        echo "<p>".$newDsc."</p>";
    }

?>
<body>
</html>

这是connect.php:

<?php
$name = $_POST["name"];
$lnk = $_POST["img"];
$dsc = $_POST["dsc"];

$servername = 'localhost';
$username = 'root';
$password = '';
$database = 'cero_db';



$conn = new mysqli($servername, $username, $password,$database);



$sql = "INSERT INTO pg (Name, Link, Description)
        VALUES ('".$name."', '".$lnk."', '".$dsc."')";
$conn->query($sql);

?>

有什么建议吗?

P.S。我找不到任何帮助我的问题,所以如果重复只是链接原文,我会删除它。

3 个答案:

答案 0 :(得分:1)

试试这个

header("Location: simple-form.php");

您必须在connect.php中添加此内容,因为表单中的提交按钮会引导您connect.php

你的connect.php应如下所示:3

<?php
   $name = $_POST["name"];
   $lnk = $_POST["img"];
   $dsc = $_POST["dsc"];

   $servername = 'localhost';
   $username = 'root';
   $password = '';
   $database = 'cero_db';

   $conn = new mysqli($servername, $username, $password,$database);



   $sql = "INSERT INTO pg (Name, Link, Description)
           VALUES ('".$name."', '".$lnk."', '".$dsc."')";
   $conn->query($sql);

   header("Location: simple-form.php");
?>

答案 1 :(得分:0)

我会在它被评论堵塞之前写在这里:

您需要将标题函数移动到将发送到浏览器的任何内容之上。由于您在调用标头之前发送了一些html,因此标头已经与您的html代码一起发送。 了解它http://php.net/manual/en/function.header.php 它在SO上被广泛询问以及搜索php头重定向

因此,您需要处理您发送任何输出或使用输出缓冲的表单。我建议您查看http://php.net/manual/en/function.ob-start.php

您还可以使用javascript将用户发送到正确的位置。

答案 2 :(得分:0)

代码的示例示例...

<强>简单-form.php的

<!DOCTYPE html>
<html>
    <head>
        <title> Sample Form </title>
        <link href = "stl.css" rel = "stylesheet" type="text/css" />
    </head>

    <body>
        <form method="post" action="connect.php">
            <input class = "PaganForm" id = "name" type="text" name="name" value="">
            <input class = "PaganForm" id = "img" type="text" name="img" value="">
            <input class = "PaganForm" id = "dsc" type="text" name="dsc" value="">
            <input type="submit" id = "SubmitButton" >
        </form>
    <body>
</html>

<强> connect.php

<?php
    $servername = 'localhost';
    $username = 'root';
    $password = '';
    $database = 'cero_db';
    $conn = new mysqli($servername, $username, $password,$database);

    $sql = "SELECT * FROM pg";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        $newName = $row['Name'];
        $newLink = $row['Link'];
        $newDsc = $row['Description'];

        echo "<h1>".$newName."</h1>";
        echo "<img src = \"".$newLink."\" />";
        echo "<p>".$newDsc."</p>";
    }

    header("Location: simple-form.php");
    exit;
?>