【发布时间】:2018-03-28 10:22:24
【问题描述】:
我最近问了一个问题,关于将 POST 变量从 jquery 发送到另一个 php 页面,重定向到该页面,对发送的信息执行一些操作,然后重定向到原始页面。
jQuery post method and directory confusion
我正在就几乎完全相同的代码寻求帮助,因为评论它的人只回答了我的一半问题并将其标记为重复。虽然它确实有帮助,但并没有解决问题。
我已根据之前尝试解决此问题的建议进行了编辑。如您所见,在此示例中点击保存按钮将带您到第一个重定向 changePage.inc.php,但不会发生第二个重定向,这应该带您到 secondPage.php。
名为 index.php 的文件
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
<script>
$(document).ready(function(){
$('#saveForm').submit(function(event){
event.preventDefault();
var message = "";
var changeText = function(){
message = "You wrote: " + $('#text').val();
};
var send = function(){
if(message !== ""){
$.post("includes/changePage.inc.php", {message: message});
window.location.href = "includes/changePage.inc.php";
}
};
changeText();
send();
});
});
</script>
<body>
<textArea id="text"></textArea>
<form id="saveForm" action="includes/saveEssay.inc.php" method="POST">
<button type="submit" id="saveButton">Save!</button>
</form>
</body>
</html>
名为 changePage.inc.php 的文件(在名为 includes 的文件夹中)
<?php
session_start();
if(isset($_POST['message'])){
header("Location: ../secondPage.php");
}
exit();
名为 secondPage.php 的文件
<?php
echo 'Hello World';
?>
【问题讨论】:
-
changePage.inc.php 的代码在哪里?
-
我很抱歉,应该是 changePage.inc.php 而不是 changeDirectory.inc.php。我将进行编辑。
-
请问,为什么要在提交之前以任何方式“格式化”信息?您可以在目标页面中执行此操作,例如
changePage.inc.php。这样你就可以完全避免 jquery 部分。 -
那么
secondPage.php和includes/saveEssay.inc.php一样吗?你没有提到includes/saveEssay.inc.phpanywhere。 -
您在这两个问题中的解释都令人困惑,因为您指的是自己的解决方案,这似乎不是一个顺利的解决方案。所以,如果你想要一个可靠的解决方案,请接受我的建议:忘记 jquery,忘记 php。只需用文字描述您的确切问题和您想要完成的工作流程。但在您的描述中尽可能使用页面名称,而不是像
the page from the php file within the includes folder, so I do not want to use jquery to redirect这样的名称。谢谢。
标签: php jquery redirect post isset