PHP Error: Undefined index: event_list when I think its already defined.
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 .= " ";
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.
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
精彩评论