javascript - 无法在 html 中显示 POSTed PHP 变量
问题描述
我正在制作一个简单的应用程序,它允许用户查看和编辑数据库中的项目,并添加新的项目。
我希望用户能够在我的“index.php”页面上单击一个项目,比如“Fiddle”,并让它重定向到一个新页面“itemDisplay.php”,它会在其中显示信息(重量、长度, 宽度, 高度) 的项目。
这是我的下拉选择器:
索引.php
<div class="btn-group w-25">
<button
type="button"
class="btn btn-outline-dark dropdown-toggle"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
Items
</button>
<div class="dropdown-menu w-100">
<a
class="dropdown-item"
href="itemDisplay.php"
id = "Fiddle"
onclick = "getItemInfo(this.id)">
Fiddle
</a>
<a
class="dropdown-item"
href="itemDisplay.php"
id = "Dish"
onclick = "getItemInfo(this.id)">
Dish
</a>
<a
class="dropdown-item"
href="itemDisplay.php"
id = "Spoon"
onclick = "getItemInfo(this.id)">
Spoon
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="./createItem.php">Create New Item</a>
</div>
</div>
这是我在同一个文件中的“getItemInfo()”函数:
index.js
<script>
function getItemInfo(id){
$.ajax({
url:"itemDisplay.php",
type: "POST",
data: {name: JSON.stringify(id)}
})
.fail(function(){
console.log("sad");
})
.done(function(response){
console.log(response);
});
}
</script>
最后,这里是itemDisplay.php文件:
'项目显示.php'
<?php
session_start();
require_once "itemManagement/dbConnect.php";
require_once "itemManagement/items.php";
$_SESSION["name"] = json_decode($_POST["name"]);
$myItem = new Item;
$test = "test";
$itemWeight = $myItem -> getItemWeight($name);
$itemLength = $myItem -> getItemLength($name);
$itemWidth = $myItem -> getItemWidth($name);
$itemHeight = $myItem -> getItemHeight($name);
echo $_SESSION["name"];
echo $test;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="./problem4.css">
<link rel="stylesheet" href="https://unpkg.com/tachyons@4.10.0/css/tachyons.min.css"/>
<title>Item Display</title>
</head>
<body>
<header class="sans-serif">
<div class="cover bg-left bg-center-l">
<div class="pb5 pb6-m pb7-l">
<div class="tc-l mt4 mt5-m mt6-l ph3">
<h1 class="f2 f1-l fw2 black-90 mb0 lh-title">
<?php echo $_SESSION["name"]; ?>
</h1>
</div>
</div>
</div>
</header>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
如果我单击“Fiddle”,控制台中 ajax 调用的记录响应会显示这一点:
'console.log(response) 从 index.php 输出'
Fiddletest
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="./problem4.css">
<link rel="stylesheet" href="https://unpkg.com/tachyons@4.10.0/css/tachyons.min.css"/>
<title>Item Display</title>
</head>
<body>
<header class="sans-serif">
<div class="cover bg-left bg-center-l">
<div class="pb5 pb6-m pb7-l">
<div class="tc-l mt4 mt5-m mt6-l ph3">
<h1 class="f2 f1-l fw2 black-90 mb0 lh-title">
Fiddle </h1>
</div>
</div>
</div>
</header>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
我的问题是,一旦“itemDisplay.php”页面加载,屏幕上唯一呈现的就是左上角的“测试”。我完全坚持能够显示 $name 变量。
经过一番研究,我了解到请求之间没有保存变量,我认为这是我的问题,所以我将 $name 变量更改为会话变量。这似乎没有任何影响。
我还尝试设置一个条件来仅呈现 $test 变量 if 'isset($_SESSION["name])',以测试该变量是否因为它以某种方式为 NULL 而没有显示,但这仍然只是呈现“test”。
我现在很困惑,因为似乎该变量确实是在该页面加载时设置的,但我无权访问它。
任何帮助将不胜感激!
解决方案
当您点击指向 的链接时item-display
,$_POST['name']
未设置,因此您分配null
给$_SESSION['name']
。您需要在分配之前检查是否设置了 POST 变量。
if (isset($_POST['name']) {
$_SESSION['name'] = $_POST['name'];
}
请注意,您先使用 AJAX 然后链接的方法是不安全的。由于 AJAX 是异步的,因此无法保证 AJAX 请求会在您链接到页面之前完成,因此可能尚未设置会话变量。最好在.done()
函数中重定向:
.done(function() {
location.href = "itemDisplay.php?name=Dish";
})
我不确定您为什么觉得需要为此使用 AJAX。您可以将参数放在 URL 中,如下所示:
<a
class="dropdown-item"
href="itemDisplay.php?name=Dish"
id = "Dish">
Dish
</a>
然后$_GET['name']
在脚本中使用。
推荐阅读
- javascript - await 单个 Promise 与异步迭代器的异步解析顺序
- google-colaboratory - 机器学习和模型训练
- google-apps-script - 我正在使用取决于相对列的 If 语句 - 无法使其工作
- python - 如果两列在三列中具有相同的值,则无法在 pandas 中获得结果,然后保留第一个 col 值,否则其他值取决于条件
- d - 如何覆盖默认结构构造函数
- selenium - 为什么 Selenium webdriver 没有打开网页?
- swift - 无法将类型“()”的返回表达式转换为返回类型“T”
- html - 如果单词内容增加,如何到达p标签的下一行
- ios - SwiftUI 中的平面导航最佳实践
- sql - T-SQL:执行批处理时出错。错误消息是:从 MyTable 中选择 * 的算术溢出