gpt4 book ai didi

javascript - Jquery $.post 没有按预期工作

转载 作者:可可西里 更新时间:2023-11-01 10:28:27 29 4
gpt4 key购买 nike

我正在使用 PHPJQueryMongoDB 创建一个网络应用程序,并且在使用 $ 时遇到了一些问题。发布。我有一个带有表格的页面,其中每一行都由一个用 CSS 设计的自定义按钮标记。我使用以下脚本获取该按钮的索引并将其传递给 PHP 文件。这将获得正确的索引并将其发布到文件中就好了。

<script>
$(".completed").click(function() {
var index = $(".completed").index(this);
$.post("lock.php", {position: index});
});
</script>

lock.php 应该将此索引放入 session 变量并加载名为 display_page.php 的新页面,但 display_page 永远不会被加载并且 Firebug 中没有显示错误。我试过直接发布到 display_page 但这并没有改变任何东西。这是我的 lock.php 的样子:

<?php
$pos = $_POST['position'];
session_start();
$_SESSION['index'] = $pos;
header('Location: display_page.php');
?>

如果有人能指出我正确的方向,我将不胜感激。

最佳答案

您需要执行以下操作:-

<script>
$(".completed").click(function() {
var index = $(".completed").index(this);
$.post("lock.php", {position: index},function( data ){
if(data == 'success'){ // check the response
window.location.href = 'display_page.php'; // if response is success then redirect to the page
}else{
// some alert message
}
});
});
</script>

在 php 页面中:-

<?php
session_start(); // first write this session start code
if(isset($_POST['position'])){ // check values are coming or not
$pos = $_POST['position'];
$_SESSION['index'] = $pos;
echo "success"; // return success to ajax
}else{
echo "failure"; // return failure to ajax
}
?>

关于javascript - Jquery $.post 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758717/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com