我的 header("Location:index.php?action=messagesent") 有问题,在用户按下提交并且 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;
?>