在提交时检查“clearYes”单选按钮时,我正在努力清除阵列。它似乎很清楚,但只要我列出数组的内容,一切都会再次出现。
这是代码
<?php
session_start();
// Initialize an array for answers
if (!isset($_SESSION['songArr']))
$_SESSION['songArr'] = array();
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<form name="songform" action="" onsubmit=" return validateForm()" method="post">
<table>
<tr>
<th>Song Name:</th>
<th><input type="text" name="songName" size="20"></th>
</tr>
<tr>
<th>Composer:</th>
<th><input type="text" name="composer" size="20"></th>
</tr>
<tr>
<th>Artist or Group:</th>
<th><input type="text" name="artist" size="20"></th>
</tr>
<tr>
<p><input name="radio1" type="radio" value="listSongs"> Show the list of songs? </p>
</tr>
</table>
<p><input name="submit" type="submit" value="Submit Song" /> <input type="reset" value="Clear form" /></p>
<p><input name="radio2" type="radio" value="clearNo" checked="checked"> Don't clear list. <input name="radio2" type="radio" value="clearYes">Clear list after submit.</p>
</form>
</body>
<?php
if (!empty($_POST['submit'])) {
// Push the posted data into the session array
$_SESSION['songArr'][] = $_POST;
}
$listSongs= $_POST['radio1'];
// Display the data now
//if clearYes is selected
if ($listSongs == "listSongs") {
foreach($_SESSION['songArr'] as $array) {
echo "<strong>Song name</strong>: {$array['songName']}<br>";
echo "<strong>Composer</strong>: {$array['composer']}<br>";
echo "<strong>Artists</strong>: {$array['artist']}<br><br>";
}
}
//if clearYes is selected
$resetArr= $_POST['radio1'];
if ($resetArr == "clearYes") {
unset($array);
$array = array();
session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
}
?>
</html>
答案 0 :(得分:1)
您需要取消设置源数组:
替换:
unset($array);
使用:
unset($_SESSION['songArr']);
希望它有所帮助。