dongyulan6251 2014-07-19 23:35
浏览 15
已采纳

PHP - 奇怪的一个

I am trying to create a "find the odd word" application (given a list of words like cat, dog, bird and car, the latter is the odd one because it's not an animal).

Firstly, I retrieve and shuffle randomly five words from the DB. They are called: odd (which is the odd one), one, two, three, four (which are the other four).

Then, I produce a form (radio button) with the five words, so that users can select one of their choice:

$words = array
(
    $odd,
    $one,
    $two,
    $three,
    $four,
);
shuffle($words);

foreach ($words as $word)
   {
     $string = $word;
     echo '<html><input type="radio" name="odd" value="'.$string.'">'.$string.'<br><br></html>';
   }

In the next PHP page, I want to check if the selected word is the odd one. I can't figure out how to do it. Any suggestions? Thanks!

  • 写回答

3条回答 默认 最新

  • dongxun1142 2014-07-19 23:48
    关注

    Use the $_SESSION variable to handle this to find out if the odd was selected or not

    Say the following code is from your Odd.php that displays the radio buttons (assuming you would handle the form element and submit button)

    <?php
        session_start();
        $_SESSION['odd'] = $odd;
    
        $words = array
        (
            $odd,
            $one,
            $two,
            $three,
            $four,
        );
    
        shuffle($words);
        echo '<form method="POST" action="Next.php">';
        foreach ($words as $word)
        {
            $string = $word;
            echo '<input type="radio" name="odd" value="'.$string.'">'.$string.'<br><br>';
        }
        echo '<input type="submit" /></form>';
    ?>
    

    On your Next.php file use the code below to validate if the odd was selected or not

    <?php
        session_start();
        $odd = $_SESSION['odd'];
        if ($_REQUEST['odd'] == $odd) { // $_REQUEST handles both $_POST and $_GET
            echo "Odd was selected";
        } else {
            echo "Odd was not selected";
        }
    ?>
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序