[英]PHP header() will not redirect issue
我的header(“ Location:index.php?action = messagesent”)头出现问题,在用户按下Submit并运行php后,它不会重定向。 通常它会重定向,但由于某种原因它不起作用,在他们点击提交后,它会重新加载页面。 但是它确实在标题代码下回显“已发送消息”。 为什么不重定向的任何想法? 先感谢您。
这是我的代码:
<form action="" method="post">
<div class="form-group">
<label for="message">Reply</label>
<textarea class="form-control" id="message" name="message" rows="5"></textarea>
</div>
<button type="submit" name="sendmessage" class="btn btn-purple waves-effect waves-light">Send Message </button>
</form>
</div>
</div>
<?php
$servername = "localhost";
$username = "myusername";
$password = 'password';
$dbname = "database";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO messaging
(fromid, toid, message, tousername,
fromusername,userprofile, sentdate, messagebefore)
VALUES (:fromid, :toid, :message, :tousername,
:fromusername, :userprofile, :sentdate, :messagebefore)");
// insert a row
$toid = $fromid;
$fromid = $_SESSION['user']['id'];
$messagebefore = $message;
$message = $_POST['message'];
$tousername = $row['fromusername'];
$fromusername = $_SESSION['user']['username'];
$userprofile = $row['userprofile'];
$sentdate = $date = date('H:i, jS F Y');
//Bind Inputs
$stmt->bindParam(':fromid', $fromid);
$stmt->bindParam(':toid', $toid);
$stmt->bindParam(':message', $message);
$stmt->bindParam(':tousername', $tousername);
$stmt->bindParam(':fromusername', $fromusername);
$stmt->bindParam(':userprofile', $userprofile);
$stmt->bindParam(':sentdate', $sentdate);
$stmt->bindParam(':messagebefore', $messagebefore);
$stmt->execute();
echo "Message Sent. ";
header("Location: inbox.php?action=messagesent");
}
catch(PDOException $e){
}
$conn = null;
?>
header(Location: ...)
仅在尚未将输出发送到浏览器时才起作用。
在脚本的前面,您输出了表单,因此header()
失败,并显示一条错误消息。
如果您查看错误日志,则会看到错误消息。
例如,在打开PHP标记后立即进行测试时,将错误报告添加到文件顶部
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
现在,您将在浏览器页面上看到错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.