0

重複の可能性:
PHP: 「通知: 未定義の変数」および「通知: 未定義のインデックス」

私はこの通知を受け取っています:

PHP 通知: 未定義のインデックス: 2 行目の /home/.../savevote.php 内の votefor

どうすればこれを修正できますか?

savevote.php の 2 行目は次のとおりです。

<?php
  $votefor = $_REQUEST["votefor"];

ありがとうございました

コードは次のとおりです。

<?php
  $votefor = $_REQUEST["votefor"];

  // Returns a random RGB color (used to color the vote bars)
  function getRandomColor()
  {
       $r = rand(128,255); 
       $g = rand(128,255); 
       $b = rand(128,255); 
       $color = dechex($r) . dechex($g) . dechex($b);
       echo "$color";
  }
  //Get the IP of the user
  $domain = $_SERVER["REMOTE_ADDR"];
  $today = date("m/d/Y");

  echo "<table id=\"tblResults\" align=\"center\">";

  //If votefor is null, then we're just viewing results, so don't log the IP
  if ($votefor != "")
  {

    //Load the addresses XML file
    $doc = new DOMDocument();
    $doc->load("../vote_dir/xml/addresses.xml");
    $addresses = $doc->getElementsByTagName("address");
    $pVoted = false;
    $pFound = false;

    //Loop through the addresses nodes and see if the person has voted before
    foreach( $addresses as $address )
    {
    $lastvisits = $address->getElementsByTagName("lastvisit");
    $lastvisit = $lastvisits->item(0)->nodeValue;

    $ips = $address->getElementsByTagName("ip");
    $ip = $ips->item(0)->nodeValue;

    if ($ip == $domain)
    {
         $pFound = true;
         if ($lastvisit == $today)
              $pVoted = true;
         else
         {
        $lastvisits->item(0)->nodeValue = $today;
        $doc->save("../vote_dir/xml/addresses.xml");
         }
         break;
    }
    else
         continue;
    }

    if ($pVoted == true) //Already voted
    {
        echo "<tr><td colspan=\"3\" class=\"message\">Έχετε ήδη ψηφίσει!</td></tr>";
    }
    else //Update the XML files
    {
    if ($pFound == false) //Add new node for IP and date to addresses.xml
    {
         echo "<tr><td colspan=\"3\" class=\"message\">Ευχαριστούμε που ψηφίσατε!</td></tr>";

         $newAddy = $doc->getElementsByTagName('addresses')->item(0);
         $newAddressElement = $doc->createElement('address');

         $newLastVisitElement = $doc->createElement('lastvisit');
         $newAddressElement->appendChild($newLastVisitElement);
         $newIPElement = $doc->createElement('ip');
         $newAddressElement->appendChild($newIPElement);

         $dayvalue = $doc->createTextNode($today);
         $dayvalue = $newLastVisitElement->appendChild($dayvalue);

         $ipvalue = $doc->createTextNode($domain);
         $ipvalue = $newIPElement->appendChild($ipvalue);

         $newAddy->appendChild($newAddressElement);

         $doc->save("../vote_dir/xml/addresses.xml");
    }
    else
    {
         echo "<tr><td colspan=\"3\" class=\"message\">Ευχαριστούμε για την ψήφο σας.</td></tr>";
    }
    // Update the vote
    $doc = new DOMDocument();
    $doc->load("../vote_dir/xml/results.xml");
    $pollitems = $doc->getElementsByTagName("pollitem");
    foreach( $pollitems as $pollitem )
    {
        $entries = $pollitem->getElementsByTagName("entryname");
        $entry = $entries->item(0)->nodeValue;
        if ($entry == $votefor)
        {
             $votes = $pollitem->getElementsByTagName("votes");
             $count = $votes->item(0)->nodeValue;
             $votes->item(0)->nodeValue = $count + 1;
             break;
        }
    }
    $doc->save("../vote_dir/xml/results.xml");
    }
  }
  else
  {
     echo "<tr><td colspan=\"3\" class=\"message\">Αποτελέσματα Ψηφοφορίας Μέχρι Στιγμής</td></tr>";
  }

  // Get max vote count
  $doc = new DOMDocument();
  $doc->load("../vote_dir/xml/results.xml");
  $maxvotes = 0;
  $pollitems = $doc->getElementsByTagName("pollitem");
  foreach( $pollitems as $pollitem )
  {
    $votes = $pollitem->getElementsByTagName("votes");
    $vote = $votes->item(0)->nodeValue;
    $maxvotes = $maxvotes + $vote;
  }
  // Generate the results table
  $doc = new DOMDocument();
  $doc->load("../vote_dir/xml/results.xml");
  $pollitems = $doc->getElementsByTagName("pollitem");
  foreach( $pollitems as $pollitem )
  {
    $entries = $pollitem->getElementsByTagName("entryname");
    $entry = $entries->item(0)->nodeValue;
    $votes = $pollitem->getElementsByTagName("votes");
    $vote = $votes->item(0)->nodeValue;
    $tempWidth = $vote / $maxvotes;
    $tempWidth = 300 * $tempWidth;
    $votepct = round(($vote / $maxvotes) * 100);
    echo "<tr><td width=\"20%\" class=\"polls\">$entry</td>";
    echo "<td width=\"30%\" class=\"resultbar\"><div class=\"bar\" style=\"background-color: ";
        getRandomColor();
        echo "; width: $tempWidth px;\">$votepct%</div></td><td width=\"20%\">($vote votes)</td></tr>";
  }
  echo "<tr><td class=\"total\" colspan=\"3\">$maxvotes άτομα ψήφισαν μέχρι τώρα.</td>";
  echo "</table>";
?>

@ブライアン

<script language="javascript">
         function setVote(voteName)
         {
            document.getElementById("votefor").value = voteName;
         }
         function confirmSubmit() 
         { 
        if (document.getElementById("votefor").value == "")
        {
             var agree=confirm("Παρακαλώ επιλέξτε μια απάντηση, για να ψηφίσετε"); 
             return false; 
        }
         } 
    </script>
4

5 に答える 5

3

リクエストパラメータが渡されていません。なぜそれが欠落している可能性があるのか​​を知っていて、通知を防ぎたいだけの場合は、次のように言うことができます。

$votefor = isset( $_REQUEST["votefor"] ) ? $_REQUEST["votefor"] : null;
于 2012-05-21T15:03:02.760 に答える
1

またはの$_REQUEST可能性があるため、使用するのは安全ではありません。どちらを使用するかを指定することをお勧めします。$_GET$_POST

次に、配列キーが存在するかどうかを確認する必要があります。これは、で公式に行うことができますarray_key_exists()。しかし、残念ながら、この関数は少し遅いです。isset()関数を使用して置き換えることができますが、これはnull値が設定されていないことを示しfalse、nullの場合に返されます。最良のアプローチは、最初にisset、次にarray_key_exists:の両方を使用することです。

<?php
if (isset($_POST['votefor']) || array_key_exists($_POST['votefor'])) {
    // do something
}
?>

またはisset、値がではないと確信している場合にのみ使用してくださいnull


インデックスが配列に存在することをほぼ100%確信している場合voteforは、それをデバッグする必要があります。var_dump配列を使用して、$_REQUESTそこにあるアイテム(およびインデックス)を確認し、間違ったことを確認します。

于 2012-05-21T15:04:15.577 に答える
0

そのように処理します。

if(array_key_exists("votefor", $_REQUEST)) {
...
}
于 2012-05-21T15:02:03.387 に答える
0

1/ という名前のリクエスト パラメータがあることを確認しますvotefor

2/ スペルを間違えていないか確認してください

于 2012-05-21T14:59:46.743 に答える
-2

error_reporting noを設定して通知レポートを表示しないようにすると、通知レポートを無視できます。

error_reporting(E_ALL & ~E_NOTICE);

于 2012-05-21T15:01:50.017 に答える