My multi-form pages works great until I unset a variable. Why?

吃可爱长大的小学妹 提交于 2021-01-29 14:00:48

问题


I have been working on a multi-form page for a while. I need some help trying to understand why when I unset my variable that the multi-form pages ceases to work correctly. I placed an echo statement in the code to track the variable value and even after a page refresh the variable stays. I literally, have to delete the cookies for the site in order for the variable to be unset.

Here is the code:

<?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>

When I add a $_SESSION["formatPickleball"] = ""; to the top of the php code, the multi-form fails to work correctly.

I have two questions 1) How can I unset variable without having to delete cookies? 2) is this the most efficient way to write this code in PHP?


回答1:


Values stored in the session must be discarded at the end of the script execution.

  1. Destroy $_SESSION using unset($ _ SESSION) and
  2. then destroy the session itself using session_destroy();



回答2:


I was able to clear the variable by using a windows popup once an if statement was fullfilled.

if ($Num_CTeams > 1 && $_SESSION["Pickleball"] !="") {
    echo '<script language="javascript">';
    echo 'alert("Sorry, Captains can only captain up to two teams ONLY.")';
    echo '</script>';
    $_SESSION["formatPickleball"] = "";
} 



回答3:


<?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" : "";
}


来源:https://stackoverflow.com/questions/60688195/my-multi-form-pages-works-great-until-i-unset-a-variable-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!