当您尝试获取 $_POST['forward'] 的值时,您会得到未定义的索引,此时代码如下:
<form name="delete" action="deleted.php" method="post">
<?php
$connect = mysql_connect("a","b","") or die("Error connecting");
mysql_select_db("c") or die("Error connecting to database");
$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
echo "<select name='forward'>";
while ($row = mysql_fetch_array($result))
{
echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";
}
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
</form>
当做这样的代码时:(我已经注释掉了数据库部分)
<form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
echo "<select name='forward'>";
echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
</form>
您不会在您的 deleted.php 中收到未定义的索引警告。
使用以下代码,您也不会收到索引警告。
<form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
$row['u'] = 2; //Dummy
echo "<select name='forward'>";
echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";
echo "<option class='class' name=" .$row['u'] . ">".$row['u'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
</form>
AND $_POST['forward']实际上会在未设置值时返回选项的名称。(1 或 2)。但是你应该像这样使用 value="" :
<form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
$row['u'] = 2; //Dummy
echo "<select name='forward'>";
echo "<option class='class' value=" .$row['t'] . ">".$row['t'] ."</option>";
echo "<option class='class' value=" .$row['u'] . ">".$row['u'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
</form>
但是,当您在代码中没有任何选项时(在下面注释掉),您将收到未定义的索引警告。
<form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
$row['u'] = 2; //Dummy
echo "<select name='forward'>";
//echo "<option class='class' value=" .$row['t'] . ">".$row['t'] ."</option>";
//echo "<option class='class' value=" .$row['u'] . ">".$row['u'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
</form>
这是因为选择列表不包含任何内容(没有选项)。
因此我的问题是:您的选择列表中实际上有任何选项吗?
试试这段代码,看看echo print_r($row,true);
会给你带来什么:
<form name="delete" action="deleted.php" method="post">
<?php
$connect = mysql_connect("a","b","") or die("Error connecting");
mysql_select_db("c") or die("Error connecting to database");
$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
/*debugging start
$row = mysql_fetch_array($result)
echo print_r($row,true);
debugging end */
echo "<select name='forward'>";
while ($row = mysql_fetch_array($result))
{
echo "<option class='class' value=\"" .$row['t'] . "\">".$row['t'] ."</option>";
}
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
</form>