猿问

在我取消设置变量之前,我的多表单页面运行良好。为什么?

我已经在多表单页面上工作了一段时间。我需要一些帮助来了解为什么当我取消设置我的变量时多表单页面停止正常工作。我在代码中放置了一个 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 编写此代码的最有效方法吗?


喵喔喔
浏览 112回答 3
3回答

缥缈止盈

一旦 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;

白板的微信

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

ITMISS

<?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
我要回答