當我提交一個form.php它調用insert.php。 insert.php將表單數據插入到數據庫中,並回顯「成功添加記錄」消息。我希望在顯示此消息後,自動調用返回到form.php。我還評論了我必須重定向的行。 想要自動重定向到一些頁面在php
My form.php
<html>
<body>
<form action="insert.php" method="post">
Name: <input type="text" name="myname"><br>
Comment: <input type="text" name="mycomment"><br>
<input type="submit">
</form>
</body>
</html>
My insert.php
<?php
$con=mysqli_connect("localhost","root","","commentdb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['myname']);
$lastname = mysqli_real_escape_string($con, $_POST['mycomment']);
$sql="INSERT INTO person (Name, Comment, Time)
VALUES ('$firstname', '$lastname', NOW())";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
// Here it should retrieve
mysqli_close($con);
?>
您可以使用標題,但是您也將無法回顯消息,因爲這將在標題之前輸出。如果你想同時回聲和重定向,你將需要使用JS或元刷新。 – 2014-09-25 08:00:40