3 回答
TA贡献1846条经验 获得超7个赞
您可以在index.js其中添加和添加自定义Cookie,并在向其document.cookie发出请求edit.php时将其提供。
index.js
const custom_cookie = "x=5";
document.cookie = custom_cookie;
.....
window.location = 'edit.php';
edit.php
$cookie_name = 'x';
echo $_COOKIE[$cookie_name]
TA贡献1906条经验 获得超3个赞
如果参数必须在UI中保持不可见(即不在地址栏中),并且由于页面重新加载而需要避免POST请求,则可以通过两个请求来实现此目的...
一个AJAX POST然后一个GET,即
fetch('set-session-var.php', {
method: 'post',
body: 'x=5',
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(res => {
if (res.ok) {
window.location = 'edit.php'
}
})
或使用发布/重定向/获取模式,例如
<form action="set-session-var.php" method="post">
<!-- set the "x" value however and whenever you want -->
<button type="submit" name="x" value="5">Go</button>
</form>
// set-session-var.php
session_start();
$_SESSION['x'] = $_POST['x'];
header('Location: edit.php'); // omit this if using the AJAX version
添加回答
举报
