douyi8408 2011-05-11 10:36 采纳率: 0%
浏览 54
已采纳

PHP错误:未定义的索引:event_list,当我认为它已经定义。

I am a complete noob in PHP and programming as well. I am very new to programming so my question might look stupid, but please be patient.

I am having an undefined index error in which I think its already defined.

I have here the codes.

index.php

<?php include('functions.php'); ?>

<?php 
 $yr = $_GET['year_list'];
 $evnt = $_GET['event_list']; 
?>

<html>
<head>
 <script type="text/javascript" src="myscripts.js"></script>
</head>
<body>
 <div>
  <form name="myform" >
   Select Year: <?php echo hspacer(1); ?>
   <select id="year_list" name="year_list">
   <?php  
    for($year = (date('Y') - 100); $year <= (date('Y') + 100); $year++ ) {
     if ($year == date('Y'))  echo "<option value='$year' name='$year' selected='' >" . $year . "</option>";
     else echo "<option value='$year' name='$year' >" . $year . "</option>";
    }
   ?>
   </select>
   <?php echo hspacer(5); ?>
   Select Event:  <?php echo hspacer(1); ?>
   <select id="event_list" name="event_list" >
   <?php  
    $events = array("Karate Tournament", "Beauty Pageant", "Film Festival", "Singing Contest", "Wedding");

    foreach($events as $event) echo "<option value='$event' name='$event' >" . $event . "</option>";
   ?>
   </select>
   <?php echo vspacer(2); echo hspacer(22); ?>
   <input type="submit" id="add_description" name="add_description" value="Add Description" onclick=""/> 
  </form>
 </div>
</body>
</html>


functions.php

<?php
 function hspacer($num_of_spaces) {
  $spaces = "";
  if ($num_of_spaces > 0)  for($i=0; $i<$num_of_spaces; $i++ )  $spaces .= "&nbsp;";

  return $spaces;
 }

 function vspacer($num_of_linefeeds) {
  $linefeeds = "";
  if ($num_of_linefeeds > 0)  for($i=0; $i<$num_of_linefeeds; $i++ )  $linefeeds .= "<br />";

  return $linefeeds;
 }
?>

What I don't understand about this, is that I think when you declared an element with an ID you can use the ID as index for $_GET or $_POST. Secondly, why is it that the second element (event_list) is not recognized when they have the same declaration as the first element which is (year_list). I'm confused with the inconsistency. Only the second element is not recognized while the other first one is recognized. Can somebody please explain to me in a simple way that a beginner like me can understand.

  • 写回答

1条回答 默认 最新

  • doulan7166 2011-05-11 10:48
    关注

    Could it be that you have &year_list allready in your url, when accessing that page? For otherwise there would be no reason to not get the same error when accessing $_GET['year_list'];

    And generally you should check your access to _GET/_POST etc. instead of just assuming the keys are set.

    Try:

    $yr = isset($_GET['year_list']) ? $_GET['year_list'] : null;
    $evnt = isset($_GET['event_list']) ? $_GET['event_list'] : null; 
    

    You could also build a function to reduce some of the code. Also have a look at:

    http://php.net/manual/en/ref.filter.php and http://www.php.net/manual/en/ini.core.php#ini.register-globals

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计