5

私はファイル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を呼び出すと、見つかったフォームのリストがドロップダウンリストに表示されます。

これはFirefoxで正常に機能します。私の問題は、internetexplorerでindex.phpを呼び出すと、次のエラーが発生することです。

Notice: Undefined index: selection in C:\path\showList.php on line 43

43行目は次のとおりです。

if($_POST['selection']==$selection_id)$selElement="selected";

上記のフォームでわかるように。何か案が?

4

2 に答える 2

2

問題の行を次のように変更する必要があります。

if($_POST['selection']==$selection_id)$selElement="selected";

に:

if(isset($_POST['selection']) && ($_POST['selection']==$selection_id))
    $selElement="selected";

(@ b1onicが提案したように)の値を確認します。

明らかに、フォームがブラウザに最初に表示されたとき(使用しているブラウザに関係なく)は何もPOSTされないため、そのエラーが発生します。

于 2012-04-04T16:55:56.317 に答える
2

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()を使用すると、より高速で簡単になります。

于 2012-04-04T16:54:17.423 に答える