我的项目之一是让某人能够在此 HTML 页面上选择选项,然后让该人单击“输入”,它会更新新选项卡的 URL。关键是将信息保存到 PHP session 中,这样如果其他人输入相同的 PHP URL,变量仍将存在于消息中,而不仅仅是保存在一台 PC 上。这就是我需要帮助的地方。
有人可以看看我的 PHP 代码并告诉我我哪里做错了吗?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" type="text/javascript">
function dynamicdropdown(listindex) {
document.getElementById('senator').className = listindex;
}
</script>
<style>
optgroup {
display: none;
}
select.agriculture
optgroup.agriculture,
select.appropriations
optgroup.appropriations
{
display: block;
}
div#header{
padding: 1px;
color: yellow;
padding-left: 9px;
background-color: #000080;
}
.category_div{
padding: 3px;
}
.sub_category_div{
padding: 3px;
}
.microphone{
padding: 3px;
}
.category_div_1{
padding: 3px;
}
.body{
padding-right: 5px;
}
</style>
</head>
<body>
<div class="header" id="header">
<h1>Testing Header</h1>
</div>
<div class="room130">
<h3>Room 130</h3>
<form target="Room 130" action = "test.php" method="POST">
<div class="category_div" id="category_div">Committee:
<select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<option value="">Select Committee</option>
<option value="agriculture">AGRICULTURE</option>
<option value="appropriations">APPROPRIATIONS</option>
</select>
</div>
<div class="sub_category_div" id="sub_category_div">
Individual:
<select name="senator" id="senator">
<option value="">Select individual</option>
<optgroup class="agriculture">
<option value="THE CHAIR">THE CHAIR</option>
<option value="THE PRESENTER">THE PRESENTER</option>
</optgroup>
</select>
</div>
<div class="microphone" id="microphone">Microphone:
<select id="microphone" name = "microphone">
<option value=" "> </option>
<option value="ON">ON</option>
<option value="OFF">OFF</option>
</select>
</div>
<input type="submit" value="Submit">
</form>
</div>
PHP 代码:
<!DOCTYPE html>
<html>
<head>
<title>Room 130</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$('#java').delay("90000").fadeOut();
});
</script>
<style>
body {
background-color: #000080;
color: white;
font-size: 72px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
font-weight: bold;
}
.java {
width: 90%;
margin: 200px;
padding-top: 100px;
text-align: center;
}
</style>
</head>
<body>
<div class="java" id="java">
<?php
$_SESSION["senator"] = $_POST["senator"];
$_SESSION["microphone"] = $_POST["microphone"];
echo "Please remind ". $_SESSION["senator"]. " to make sure their microphone is " .$_SESSION["microphone"]. ". Thank you.";
?>
</div>
</body>
</html>
最佳答案
首先,要存储和检索 session 变量,您需要使用session_start()启动 session 。 .
其次, session 标识符本地存储在 cookie 中,任何试图规避这一点并为多个用户使用同一 session 的尝试都会带来严重的麻烦,并可能存在安全漏洞,这是不值得的。
如果您需要在服务器端存储值,请考虑使用数据库。对于极其简单的任务,您也可以使用纯文本存储,但通常不建议这样做。
现在,当涉及到您想要实现的特定任务时,HTTP GET 变量似乎正是您所需要的:
在第一页中替换这一行:
<form target="Room 130" action = "test.php" method="POST">
这样:
<form target="Room 130" action="test.php" method="GET">
在 test.php
中替换这些行:
$_SESSION["senator"] = $_POST["senator"];
$_SESSION["microphone"] = $_POST["microphone"];
echo "Please remind ". $_SESSION["senator"]. " to make sure their microphone is " .$_SESSION["microphone"]. ". Thank you.";
有了这些:
$senator = $_GET['senator'];
$microphone = $_GET['microphone'];
echo "Please remind ". $senator. " to make sure their microphone is " .$microphone. ". Thank you.";
如果 test.php
不仅通过在第一页上提交表单来访问,您可能需要检查变量是否存在:
if(isset($_GET['senator']) && isset($_GET['microphone'])) {
$senator = $_GET['senator'];
$microphone = $_GET['microphone'];
echo "Please remind ". $senator. " to make sure their microphone is " .$microphone. ". Thank you.";
} else {
// Do something when the page is accessed without any GET variables in the url
}
如果您这样做,那么当您访问 test.php?senator=Jonµphone=12
时,页面将输出以下内容:
Please remind Jon to make sure their microphone is 12. Thank you.
编辑:另一个选择是文本存储。
在包含 test.php
的同一文件夹中创建文件 data.txt
,并将权限设置为 0666
。
在 test.php
中替换这些行:
$_SESSION["senator"] = $_POST["senator"];
$_SESSION["microphone"] = $_POST["microphone"];
echo "Please remind ". $_SESSION["senator"]. " to make sure their microphone is " .$_SESSION["microphone"]. ". Thank you.";
有了这些:
if(isset($_POST["senator"]) && isset($_POST["microphone"])) { // If the page receives POST data, it needs to be stored
$senator = $_POST["senator"];
$microphone = $_POST["microphone"];
$data = json_encode(Array($senator, $microphone)); // Put values into an array and encode it for storage
file_put_contents('data.txt', $data); // Put the JSON-encoded array into a text file. This function replaces the contents of the text file, which is exactly what is needed in this application. To append instead of replacing the contents, you need a FILE_APPEND flag.
} else { // If there's no POST data, the values are retrieved from the storage
$data = json_decode(file_get_contents('data.txt')); // Retrieve the contents of the text file and decode them back into an array
$senator = $data[0];
$microphone = $data[1];
}
echo "Please remind ". $senator. " to make sure their microphone is " .$microphone. ". Thank you.";
编辑2:如果您使用上面的文本存储解决方案并希望使用JS动态更新页面,我建议使用jQuery。
由于我们已经将值存储在文本文件中,因此在 JS 中访问同一文件也是合乎逻辑的,尽管出于安全考虑您无法在本地访问它,但必须像远程资源一样访问它。
将此 JS 函数添加到您的页面:
function updateData() {
$.getJSON("https://website.com/data.txt", function(data) {
var senator = data[0];
var microphone = data[1];
$("#java").text("Please remind " + senator + " to make sure their microphone is " + microphone + ". Thank you.");
});
}
并在您想要更新数据时调用它。您可以将其放入循环中或单击时调用它。
关于javascript - PHP 代码 - 在一台 PC 上保存变量,而不仅仅是 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811473/