dqwh1209 2013-09-24 03:50
浏览 59

在表单提交上显示隐藏的div(javascript和PHP)

I'm trying to create a number guessing game:

<?php
    $num = rand(1,1000);
    $guess = $_POST["guess"];
?>

<div class="intro">We've generated a random number between 1 and 100. Give us your best guess!</div>

<form action="index.php" method="post">
    <input type="text" name="guess">
    <input type="submit" value="Guess!" onsubmit="showHide()">
</form>

<div class="guess-text">Your guess was <span class="guess-var"><?php echo $guess; ?></span></div>

<div class="echo" id="hidden_div>
<?php
    if ($guess > $random) {
    echo "Sorry, too high! Better luck next number!";
    }

elseif ($guess < $random) {
echo "Sorry, too low! Better luck next number!";
    }

else {
    echo "<script type='text/javascript'>location.href='/youwin.html'</script>";
}
?>
</div>

I want the div that displays the result

<div class="echo" id="hidden_div">

to be hidden until after the form is submitted. Here is the only code I've been able to find that helps me out:

<script type="text/javascript">
    function showHide() {
        var div = document.getElementById("hidden_div");
        if (div.style.display == 'none') {
            div.style.display = '';
        }

        else {
            div.style.display = 'none';
        }
    }
</script>

Unfortunately, this is only hiding the div. I need some help to ensure that the div will reappear after a guess is submitted. Thanks.

  • 写回答

2条回答 默认 最新

  • dshqd84261 2013-09-24 03:56
    关注

    initialy make div as hidden

    <div class="echo" id="hidden_div" style="display:none">
    <?php
        if ($guess > $random) {
        echo "Sorry, too high! Better luck next number!";
        }
    
    elseif ($guess < $random) {
    echo "Sorry, too low! Better luck next number!";
        }
    
    else {
        echo "<script type='text/javascript'>location.href='/youwin.html'</script>";
    }
    ?>
    </div>
    

    on button click

    <input type="submit" value="Guess!" id="btn" name="btn" onclick="showHide()">
    
    <script type="text/javascript">
        function showHide() {
                document.getElementById("hidden_div").style.display = "block";
        }
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line