我遇到了标题问题(“位置:index.php?action = messagesent”),用户按下提交并运行php后,它不会重定向。通常它会重定向但由于某种原因它不起作用,它只是在它们点击提交后重新加载页面。但它确实在标题代码下面回显“Message Sent”。关于它为什么不重定向的任何想法?提前谢谢。
继承我的守则:
<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;
?>
答案 0 :(得分:1)
header(Location: ...)
仅在您尚未向浏览器发送输出时才有效。
在您的脚本中,您输出了表单,因此header()
失败并显示错误消息。
如果您查看错误日志,您将看到错误消息。
将error reporting添加到 例如
打开PHP标记后立即测试的文件顶部<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
现在,您将在浏览器页面上看到错误。