我的多格式页面在未设置变量之前都可以正常工作。为什么?

问题描述 投票:0回答:2

我已经在多格式页面上工作了一段时间。我需要一些帮助,以了解为什么当我取消设置变量时,多格式页面无法正常工作。我在代码中放置了echo语句以跟踪变量值,甚至在页面刷新后,变量仍保持不变。从字面上看,我必须删除网站的Cookie,才能取消设置变量。

这里是代码:

<?php
session_start();
// Start the session
echo $_SESSION["formatPickleball"];
$_SESSION["Pickleball"] = "";
// Submit button is pressed
if (isset($_POST['format1']))
{
    $_SESSION["formatPickleball"] = $_POST['member'];
    $_SESSION["Pickleball"] = "hide";
}
if ($_SESSION["formatPickleball"] == "Captain") {
$regCAP = "";
// Submit button is pressed
if (isset($_POST['submitCAP']))
{
$regCAP = "show";
}
$secondCAP = "";
// Submit button is pressed
if (isset($_POST['submit2']))
{
$secondCAP = "show";
}
$thirdCAP = "";
// Submit button is pressed
if (isset($_POST['submit3']))
{
$thirdCAP = "show";
}
} elseif ($_SESSION["formatPickleball"] == "Player") {
$regPlay = "";
// Submit button is pressed
if (isset($_POST['submitPlay']))
{
$regPlay = "show";
} 
}
?>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css">
    <script type="text/javascript"></script>
<body>
<div id="mainForm">
    <h4>Are you a Captain or a Player on a Team?</h4>
<form method='POST'>
    <input type="radio" name="member" value="Captain">Captain
    <input type="radio" name="member" value="Player">Player<br>
    <input type="submit" name="format1" value="Select">     
</form>
</div>

<div id="firstForm-CAP">
    <h4>Captain's Registration Form - Page 1</h4>
<form method='POST'>
    <input type="submit" name="submitCAP" value="Step 2" Style = "color:white; background-color:blue" required>     
</form>
</div>
<br>
<div id="secondForm-CAP">
    <h4>Captain Registration Form - Page 2</h4>
<form method='POST'>
    <input type="submit" class="pull-left" name="submit2" value="Next Step" Style = "color:white; background-color:blue">
</form>
</div>
<div id="thirdForm-CAP">
    <h4>Captain's Registration Form - Page 3</h4>
<form method='POST'>
    <input type="submit" class="pull-left" name="submit3" value="Next Step" Style = "color:white; background-color:blue">
</form> 
</div>  
<div id="paypalForm-CAP">
    <h4>Captain's Registration Form - Last Page 4</h4>
</div>
<div id="firstForm-Play">
    <h4>Player Registration Form - Page 1</h4>
<form method='POST'>
    <input type="submit" name="submitPlay" value="Next" Style = "color:white; background-color:blue" required>      
</form>
</div>
<br>
<div id="secondForm-Play">
<h4>Player Registration Form - Last Page 2</h4>
</div>
</body>
<?php
if($_SESSION["Pickleball"]!="" && $_SESSION["formatPickleball"] == "Captain"){
?>
<script type="text/javascript">
$("#mainForm").hide();  
$("#firstForm-CAP").show();
$("#secondForm-CAP").hide();
$("#thirdForm-CAP").hide();     
$("#paypalForm-CAP").hide();
$("#firstForm-Play").hide();
$("#secondForm-Play").hide();
</script>
<?php
}elseif($regCAP!=""){
?>
<script type="text/javascript">
$("#mainForm").hide();  
$("#firstForm-CAP").hide();
$("#secondForm-CAP").show();
$("#thirdForm-CAP").hide();     
$("#paypalForm-CAP").hide();
$("#firstForm-Play").hide();
$("#secondForm-Play").hide();
     </script>
<?php
}elseif($secondCAP!=""){
?>
<script type="text/javascript">
$("#mainForm").hide();
$("#firstForm-CAP").hide();
$("#secondForm-CAP").hide();
$("#thirdForm-CAP").show(); 
$("#paypalForm-CAP").hide();
$("#firstForm-Play").hide();
$("#secondForm-Play").hide();
</script>
<?php
}elseif($thirdCAP!=""){
?>
<script type="text/javascript">
$("#mainForm").hide();
$("#firstForm-CAP").hide();
$("#secondForm-CAP").hide();
$("#thirdForm-CAP").hide(); 
$("#paypalForm-CAP").show();
$("#firstForm-Play").hide();
$("#secondForm-Play").hide();
</script>
<?php
}elseif($_SESSION["Pickleball"]!="" && $_SESSION["formatPickleball"] == "Player"){
?>
<script type="text/javascript">
$("#mainForm").hide();
$("#firstForm-CAP").hide();
$("#secondForm-CAP").hide();
$("#thirdForm-CAP").hide(); 
$("#paypalForm-CAP").hide();
$("#firstForm-Play").show();
$("#secondForm-Play").hide();
</script>
    <?php
}elseif($regPlay!=""){
?>
<script type="text/javascript">
$("#mainForm").hide();
$("#firstForm-CAP").hide();
$("#secondForm-CAP").hide();
$("#thirdForm-CAP").hide(); 
$("#paypalForm-CAP").hide();
$("#firstForm-Play").hide();
$("#secondForm-Play").show();
</script>
    <?php
}else{
?>
<script type="text/javascript">
$("#mainForm").show();
$("#firstForm-CAP").hide();
$("#secondForm-CAP").hide();
$("#thirdForm-CAP").hide(); 
$("#paypalForm-CAP").hide();
$("#firstForm-Play").hide();
$("#secondForm-Play").hide();
</script>
<?php
}
?>
    <script type="text/javascript">
$(".chosen").chosen();
</script>
</html>

当我添加$ _SESSION [“ formatPickleball”] =“”时;到php代码的顶部,多表单无法正常工作。

我有两个问题1)如何在不删除Cookie的情况下取消设置变量? 2)这是用PHP编写此代码的最有效方法吗?

php html session-variables
2个回答
0
投票

存储在会话中的值必须在脚本执行结束时丢弃。

  1. 使用$_SESSION销毁unset($ _ SESSION)
  2. 然后使用session_destroy();销毁会话本身>

-1
投票
<?php
session_start();

echo $_SESSION["formatPickleball"];
$_SESSION["Pickleball"] = "";

if (isset($_POST['format1']) && $_POST['format1'] && isset($_POST['member']) && $_POST['member']) {
    $_SESSION["formatPickleball"] = $_POST['member'];
    $_SESSION["Pickleball"] = "hide";
}

if ($_SESSION["formatPickleball"] == "Captain") {
    $regCAP = isset($_POST['submitCAP']) && $_POST['submitCAP'] ? "show": "";
    $secondCAP = isset($_POST['submit2']) && $_POST['submit2'] ? "show": "";
    $thirdCAP = isset($_POST['submit3']) && $_POST['submit3'] ? "show": "";
} 
if ($_SESSION["formatPickleball"] == "Player") {
    $regPlay =  isset($_POST['submitPlay']) && $_POST['submitPlay'] ? "show" : "";
}
© www.soinside.com 2019 - 2024. All rights reserved.