输入值的比较

时间:2011-02-03 16:22:47

标签: php

尝试实现:如果输入“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/>";
        }
    }    

?>

3 个答案:

答案 0 :(得分: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' /> ";

通过这种方式,结果将被检查,如果它们存在,当页面加载时,可以提供输出,然后再次提出问题。

在上面的代码中,你试图“获取”值'key',它实际上并不存在(在这段代码中)。使用$ _GET和$ _POST,您可以引用前一个加载的表单字段,并按名称引用这些字段。

最后,由于电影名称是只读的,我做了一个更改,以显示如何使用隐藏的表单元素处理提交名称,而不是创建一个会让用户感到困惑的文本框。

答案 1 :(得分: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']) {
    ...
}

希望这有一定意义。

答案 2 :(得分: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/>";
        }
    }    

?>