尽管将表单发送到接收页面正常(通过$ _POST),但我想知道如何通过<a>
标签将其发送回去。是否可以不使用AJAX或任何JS脚本?我正在考虑使用cookie,但不知道如何在PHP / HTML脚本之间设置它。
这是我正在做的代码示例
问题页面:
<form action="results.php" method="POST">
<select name="SampleSelect">
<option>Sample1</option>
<option>Sample2</option>
<option>Sample3</option>
</select>
</form>
答案页:
<a href="questions.php">Return to questions</a>
<?php
$answer = $_POST['SampleSelect'];
echo $answer;
?>
答案 0 :(得分:3)
使用session_start()
开始或恢复会话,然后将答案存储在会话中。代码看起来像这样:
answer.php:
<?php
session_start();
?>
<a href="questions.php">Return to questions</a>
<?php
$answer = $_POST['SampleSelect'];
$_SESSION['answer'] = $answer;
echo $answer;
?>
question.php:
<?php
session_start();
$answer = $_SESSION['answer'];
$options = [
"Sample1",
"Sample2",
"Sample3"
];
?>
<form action="results.php" method="POST">
<select name="SampleSelect">
<?php
foreach ($options as $option) {
if ($option === $answer) {
echo '<option selected>' . $option . "</option>\n";
} else {
echo '<option>' . $answer . "</option>\n";
}
}
?>
</select>
</form>
答案 1 :(得分:0)
是的,您可以将其作为$ _GET传递
$var = 'something';
echo "<a href='questions.php?var=$var'>Pass me back</a>";
在您的问题PHP文件中,您将获得它:
$var = $_GET['var'];
echo $var;