0

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I am getting this notice:

PHP Notice: Undefined index: votefor in /home/.../savevote.php on line 2

How can I fix this?

Here is my line 2 in savevote.php:

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

Thank you

Here is the code:

<?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>
Community
  • 1
  • 1
Pavlos1316
  • 474
  • 10
  • 25

5 Answers5

3

The request parameter isn't being passed. If you know why it might be missing and just want to prevent the notice, you can say:

$votefor = isset( $_REQUEST["votefor"] ) ? $_REQUEST["votefor"] : null;
Johno
  • 1,959
  • 1
  • 15
  • 15
1

It is not safe to use $_REQUEST because it can be $_GET or $_POST, better is specify which you want.

At second you need to check if the array key exists. This can officially be done with array_key_exists(). But unfortunately this function is a little bit slow. You can replace it by using the isset() function, but this says a null value is not set and returns false when it is null. The best approach is to use them both, first the isset and then the array_key_exists:

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

Or use only isset if you are sure the value won't be null.


If you are almost 100% sure the index votefor exists in the array you need to debug it. var_dump the $_REQUEST array to see which items (and indexes) are in there and look what you did wrong.

Wouter J
  • 41,455
  • 15
  • 107
  • 112
0

1/ Check you have a request parameter named votefor

2/ Check you didn't misspelt it

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
0

Handle it like so:

if(array_key_exists("votefor", $_REQUEST)) {
...
}
nullpotent
  • 9,162
  • 1
  • 31
  • 42
-2

You may ignore notice reports by setting error_reporting no to show them

error_reporting(E_ALL & ~E_NOTICE);

Alex
  • 7,538
  • 23
  • 84
  • 152