我在文件showList.php中编写了以下表单,该表单从数据库中选择项目并将其显示在下拉列表中:
<form id="selForm" name="selForm" action="index.php" method="post">
<select name="selection" id="selection">
<option id="nothingSelected" >--Choose form---></option>
<?php
$con=mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("myDatabase",$con);
$result = mysql_query("SELECT * FROM formsTable");
while($row = mysql_fetch_array($result))
{
$selection_id=$row['id'];
if($_POST['selection']==$selection_id)$selElement="selected";
echo "<option id='$selection_id' name=\"sectionid\" value='$selection_id' >";
echo $row['nummer'] . " " . $row['titel']. " ";
echo "</option>";
}
?>
</select>
<input type="button" value="load form" onClick="validateForm(document.selForm)">
<input type="button" value="delete form" onClick="deleteForm(document.selForm);">
</form>我将此文件包含在index.php中,如下所示:
<?php include('showList.php');?>现在,当我调用index.php时,找到的表单列表将显示在下拉列表中。
这在火狐中运行得很好,我的问题是当我在internetexplorer中调用index.php时,我得到了以下错误:
Notice: Undefined index: selection in C:\path\showList.php on line 43第43行是:
if($_POST['selection']==$selection_id)$selElement="selected";正如您在上面的表单中所看到的。有什么想法吗?
发布于 2012-04-05 00:55:56
您需要将问题行从以下位置更改:
if($_POST['selection']==$selection_id)$selElement="selected";至:
if(isset($_POST['selection']) && ($_POST['selection']==$selection_id))
$selElement="selected";检查的值(如@b1onic所建议的)。
显然,当表单第一次在浏览器中显示时,没有任何东西是POSTed的--无论您使用的是哪种浏览器--所以您将收到该错误。
发布于 2012-04-05 00:54:17
似乎你的php脚本正在尝试读取你的$_POST变量中的'selection‘,但是它还没有被定义。
替换该行:
if($_POST['selection']==$selection_id)关于这个问题:
if(array_key_exists('selection', $_POST) && $_POST['selection'] == $selection_id)或
if(isset($_POST['selection']) && $_POST['selection'] == $selection_id) 这应该会修复你的警告和array_key_exists之间的差异。在这种情况下,使用isset(),因为它更快、更容易。
https://stackoverflow.com/questions/10015584
复制相似问题