2011-05-11 127 views
1

我是一個PHP和編程的完整noob。我對編程非常陌生,所以我的問題可能看起來很愚蠢,但請耐心等待。PHP錯誤:未定義索引:event_list當我認爲它已經定義。

我有一個未定義的索引錯誤,我認爲它已經定義。

我在這裏的代碼。

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

我不明白這個是什麼,是我認爲當你宣佈一個ID的元素您可以使用該ID作爲索引$_GET$_POST。其次,爲什麼第二個元素(event_list)與第一個元素(year_list)具有相同的聲明時無法識別。我對這種不一致感到困惑。只有第二個元素不被識別,而另一個元素被識別。有人可以用簡單的方式向我解釋,像我這樣的初學者可以理解。

+0

使用'的print_r($ _ GET)'和'的print_r($ _ REQUEST)',讓我知道什麼是它打印? – 2011-05-11 10:47:29

回答

1

難道這是你有&year_list已經在你的網址,當訪問該網頁?否則在訪問時沒有理由不會得到相同的錯誤$_GET['year_list'];

而且通常您應該檢查您對_GET/_POST等的訪問權限,而不是僅僅假定已設置密鑰。

嘗試:

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

你也可以建立一個功能,以減少一些代碼。也看看:

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

+0

謝謝...解決了它。 – 2011-05-11 10:56:40