在我取消设置变量之前,我的多表单页面工作得很好。为什么?

我已经在多表单页面上工作了一段时间。我需要一些帮助来尝试理解为什么当我取消设置变量时,多表单页面无法正常工作。我在代码中放置了一条 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>



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


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


慕勒3428872
浏览 107回答 3
3回答

千万里不及你

存储在会话中的值必须在脚本执行结束时被丢弃。销毁&nbsp;$_SESSION&nbsp;使用&nbsp;unset($ _ SESSION)&nbsp;和然后使用销毁会话本身&nbsp;session_destroy();

不负相思意

一旦 if 语句完成,我就可以通过使用窗口弹出窗口来清除变量。if ($Num_CTeams > 1 && $_SESSION["Pickleball"] !="") {&nbsp; &nbsp; echo '<script language="javascript">';&nbsp; &nbsp; echo 'alert("Sorry, Captains can only captain up to two teams ONLY.")';&nbsp; &nbsp; echo '</script>';&nbsp; &nbsp; $_SESSION["formatPickleball"] = "";}&nbsp;

素胚勾勒不出你

<?phpsession_start();echo $_SESSION["formatPickleball"];$_SESSION["Pickleball"] = "";if (isset($_POST['format1']) && $_POST['format1'] && isset($_POST['member']) && $_POST['member']) {&nbsp; &nbsp; $_SESSION["formatPickleball"] = $_POST['member'];&nbsp; &nbsp; $_SESSION["Pickleball"] = "hide";}if ($_SESSION["formatPickleball"] == "Captain") {&nbsp; &nbsp; $regCAP = isset($_POST['submitCAP']) && $_POST['submitCAP'] ? "show": "";&nbsp; &nbsp; $secondCAP = isset($_POST['submit2']) && $_POST['submit2'] ? "show": "";&nbsp; &nbsp; $thirdCAP = isset($_POST['submit3']) && $_POST['submit3'] ? "show": "";}&nbsp;if ($_SESSION["formatPickleball"] == "Player") {&nbsp; &nbsp; $regPlay =&nbsp; isset($_POST['submitPlay']) && $_POST['submitPlay'] ? "show" : "";}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5