2011-02-03 126 views
0

試圖執行:如果輸入「year」字段的值不是數字(NAN)javascript函數 - 不起作用。我的語法中是否有錯誤?輸入值的比較

謝謝。

<?php 

    echo "<h1>Testing your Trivia</h1>"; 
    $ages['Casablanca'] = "1943"; 
    $ages['Around The World in 80 Days'] = "1956"; 
    $ages['Patton'] = "1970"; 
    $ages['Annie Hall'] = "1977"; 
    $ages['Chariots of Fire'] = "1981"; 
    $ages['Dances With Wolves'] = "1990"; 
    $ages['Crash'] = "2005"; 
    $ages['The Departed'] = "2006"; 

    $rand_keys = array_rand($ages, 1); 

?> 
<script language="JavaScript" type="text/javascript"> 

function validate (form) 
{ 
    var valid = "1234567890"; 
    if (form.year.value == "" || isNaN(valid)) { 
    alert("Please enter a year."); 
    return false; 
    } 
    return true; 
} 
</script> 

<form method='post' name="inputyear" onsubmit="return validate(this);"> 
Give the year below won academy award<br> 
<Strong>Movie:</strong> <input type='text' name='movie' value='<?= $rand_keys ?>'  readonly='readonly' /><br> 
<Strong>Year it Won the Oscar:</Strong> <input type='text' name='year' size="30" /><br/> 
<input type='submit' name='submit' /> 
</form> 

<?php 

    echo '<pre>'; 
    foreach($ages as $movie => $year){ 
     print_r("Year: $year, Title: $movie <br />"); 
    } 
    echo '</pre>'; 

    if($_POST['submit']) { 
     $movie = $_POST['movie']; 
     $year = $_POST['year']; 
     $realyear = $ages[$movie]; 

     echo "<h2>Answer for: $movie</h2>"; 
     if(array_key_exists($movie, $ages)) { 
      echo "Your answer: $year<br/>"; 
     } 
     echo "Correct Answer: $realyear<br/>"; 
     if ($realyear == $year) { 
      echo "<strong style='color:green;'>WELL DONE</strong><br/>"; 
     } else { 
     // wrong 
      echo "<strong style='color:red;'>Incorrect.</strong><br/>"; 
     } 
    }  

?> 
+0

你想向用戶展示一些電影名稱,然後讓他猜猜電影被授予了哪一年,我理解它是對的嗎? – Chvanikoff 2011-02-03 17:06:56

回答

0

在上面的代碼中,每次加載頁面時,輸出結果都會包含所有電影和年份。由於這應該是一個測驗,我想這不會是期望的結果。我建議想是這樣的:

<?php 
    echo "<h1>Testing your Trivia</h1>"; 
    $ages['Casablanca'] = "1943"; 
    $ages['Around The World in 80 Days'] = "1956"; 
    $ages['Patton'] = "1970"; 
    $ages['Annie Hall'] = "1977"; 
    $ages['Chariots of Fire'] = "1981"; 
    $ages['Dances With Wolves'] = "1990"; 
    $ages['Crash'] = "2005"; 
    $ages['The Departed'] = "2006"; 

    if(isset($_GET['year'])){ 
     if($ages[$_GET['movieName']]==$_GET['year']){ 
      echo "Correct! {$_GET['movieName']} was made in {$_GET['year']}"; 
     }else{ 
      echo "Sorry! Your answer of {$_GET['year']} is wrong.<br/>"; 
      echo $_GET['movieName'] . " was made in {$ages[$_GET['movieName']]}"; 
      } 
    } 

    $rand_keys = array_rand($ages, 1); 

    echo "Give the year below won academy award<br>"; 
    echo "<Strong>Movie: </strong>$rand_keys <input type='hidden' name='movieName' value='$rand_keys'/><br>"; 
    echo "<Strong>Year it Won the Oscar: </Strong> <form method='get'><input type='text' name='year' /></form><input type='submit' /> "; 

這樣,結果得到檢查,如果存在的話,加載網頁時,可以提供輸出,然後再次提出這樣的問題。

在你上面的代碼你想「得到」值「鍵」,這實際上並不存在(在這一領域的代碼)。使用$ _GET和$ _POST,您可以引用上一次加載的表單字段並通過名稱引用這些字段。

最後,因爲這部電影的名字只讀的,我做了一個變化來向您展示如何處理使用隱藏的表單元素提交的名稱,而不是創建一個文本框會混淆用戶。

+0

非常好,謝謝DMC。如果你可以簡單地解釋'if($ ages [$ _ GET ['movieName']] == $ _ GET ['year'])'' – Jshee 2011-02-03 16:46:26

+0

當然可以。 $ _GET ['movieName']從窗體調用電影名稱。當我把它放在$ ages []中時,我基本上是將movieName的值傳遞給age,以便檢索數組中的正確元素。然後根據輸入人的年份測試數組的值。 – dmcnelis 2011-02-03 16:48:02

0

我不明白你的問題的措辭,但根據您的代碼,我能發現一對夫婦的誤解。

$_GET是一個數組。如果$_GET定義:

$_GET = array('movie_title' => 'Patton', 'year' => '1970'); 

然後$_GET['movie_title']'Patton'$_GET['year']'1970'

的$ _GET密鑰由輸入字段的名稱給定的,所以你需要給一個名字的電影標題輸入。

然後,詢問用戶是否選擇了一個電影,你知道奧斯卡獲獎一年,你會測試:你

if (array_key_exists($_GET['movie_title'], $ages)) { 
    ... 
} 

然後,看看他們是否得到了正確的答案,將考驗:

if ($ages[$_GET['movie_title']] == $_GET['year']) { 
    ... 
} 

希望這蘊藏着某種意義。

0
<?php 

    echo "<h1>Testing your Trivia</h1>"; 
    $ages['Casablanca'] = "1943"; 
    $ages['Around The World in 80 Days'] = "1956"; 
    $ages['Patton'] = "1970"; 
    $ages['Annie Hall'] = "1977"; 
    $ages['Chariots of Fire'] = "1981"; 
    $ages['Dances With Wolves'] = "1990"; 
    $ages['Crash'] = "2005"; 
    $ages['The Departed'] = "2006"; 

    $rand_keys = array_rand($ages, 1); 

?> 

<form method='post'> 
Give the year below won academy award<br> 
<Strong>Movie:</strong> <input type='text' name='movie' value='<?= $rand_keys ?>'  readonly='readonly' /><br> 
<Strong>Year it Won the Oscar:</Strong> <input type='text' name='year' /><br/> 
<input type='submit' name='submit' /> 
</form> 

<? 

    echo '<pre>'; 
    foreach($ages as $movie => $year){ 
     print_r("Year: $year, Title: $movie <br />"); 
    } 
    echo '</pre>'; 

    if($_POST['submit']) { 
     $movie = $_POST['movie']; 
     $year = $_POST['year']; 
     $realyear = $ages[$movie]; 

     echo "<h2>Answer for: $movie</h2>"; 
     if(array_key_exists($movie, $ages)) { 
      echo "Your answer: $year<br/>"; 
     } 
     echo "Correct Answer: $realyear<br/>"; 
     if ($realyear == $year) { 
      echo "<strong>WELL DONE</strong><br/>"; 
     } else { 
      echo "Incorrect.<br/>"; 
     } 
    }  

?>