我正在尝试为我的计算机科学课做一个骰子游戏

时间:2016-05-04 14:26:17

标签: php html

我的目标是使用下拉列表获取用户输入,以便当用户提交“猜测”时,页面会重新加载随机骰子生成器并检查用户是否“正确猜测”。我把我的代码放在下面,非常感谢任何帮助。

- (UIColor *)graphChartView:(ORKGraphChartView *)graphChartView fillColorForPlotIndex:(NSInteger)plotIndex;

1 个答案:

答案 0 :(得分:0)

以下是您的简单解决方案:

<?php
    $number     = isset($_POST['number']) ? $_POST['number'] : null;
    $roll       = rand(1, 6);
    $message    = "";

    if (!$number){
        $message.= "<p class='error'>Choose a Number!</p>";
    }else if($number == $roll){
        $message = "<p>Good Guess!</p><br>";
        $message.= "<img src='http://bit.do/IcsDice{$roll}' title='dice'>";
    }else{
        $message = "<p>You guessed incorrectly, Too Bad.</p><br>";
        $message.= "<img src='http://bit.do/IcsDice{$roll}' title='dice'>";
    }
?>

<html>
    <head>
        <script type="text/javascript">
            function submitGuessForm(sender){
                var guessForm       = document.forms[0]; //THE FIRST... & ONLY FORM IN THE PAGE...
                var selectedOption  = sender.options[sender.selectedIndex].value;

                if(!selectedOption || selectedOption == ''){
                    alert("Please, guess a Number to play the game.");
                }else{
                    guessForm.submit();
                }
                return false;
            }
        </script>
    </head>
    <body>
        <h1>Dice Game!</h1><br><br>

        <hr size="2" /><br><br>
        <p class="error-box"><strong><?php echo $message; ?></strong></p>
        <!-- SUBMIT FORM TO THE SAME SCRIPT -->
        <form name="form1" action="" method="POST"> <!-- activity-dice-game.php -->
            <label for="die_choice">
                HOW INTUITIVE ARE YOU? GUESS !!!<br />
                <select name="number" id="die_choice" class="fancyInput" onchange="submitGuessForm(this);">
                    <option value="">Choose a Number</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                </select>
            </label></br><br><br>
            <!-- NO NEED FOR THE SUBMIT BUTTON SINCE YOU CAN DIRECTLY SUBMIT THE FORM WHEN AN OPTION IS SELECTED - WITH JAVASCRIPT THOUGH -->
            <!-- <input type="submit" value="Submit" name="subBtn" class="btn"></input> -->
        </form>
    </body>
</html>