PHP未定义的索引错误。怎么修?

时间:2012-05-21 14:57:54

标签: php

  

可能重复:
  PHP: “Notice: Undefined variable” and “Notice: Undefined index”

我收到了这个通知:

  

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>";
?>

@Brian

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

5 个答案:

答案 0 :(得分:3)

未传递请求参数。如果您知道为什么它可能会丢失并且只是想阻止通知,您可以说:

$votefor = isset( $_REQUEST["votefor"] ) ? $_REQUEST["votefor"] : null;

答案 1 :(得分:1)

使用$_REQUEST是不安全的,因为它可以是$_GET$_POST,更好的是指定您想要的内容。

第二,您需要检查数组键是否存在。这可以通过array_key_exists()正式完成。但不幸的是这个功能有点慢。您可以使用isset()函数替换它,但是这表示未设置null值,并且当它为空时返回false。最好的方法是使用它们,首先是isset,然后是array_key_exists

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

如果您确定该值不是isset,则仅使用null


如果您几乎100%确定数组中存在索引votefor,则需要对其进行调试。 var_dump $_REQUEST数组,查看哪些项目(和索引)在那里,看看你做错了什么。

答案 2 :(得分:0)

1 /检查您是否有名为votefor

的请求参数

2 /检查你没有拼错它

答案 3 :(得分:0)

像这样处理:

if(array_key_exists("votefor", $_REQUEST)) {
...
}

答案 4 :(得分:-2)

您可以通过设置error_reporting no来显示它们来忽略通知报告

error_reporting(E_ALL & ~E_NOTICE);