刷新问题?脚本在每次刷新时都会继续运行

时间:2016-04-24 03:56:56

标签: javascript php html file page-refresh

每次刷新时都会删除文件中的txt。每当玩家赢得我的游戏时,我只需要运行这个脚本。但它似乎每次刷新都会运行。

这是索引

<?php
    $win = 0;
    $cards = 5;
    $probability = 0.5;

    function generateScratchCard($n,$p) {
        $w = 0;
        for($i = 0; $i<$n; $i++) {
            $result = (mt_rand() / mt_getrandmax());
            if($result >= $p) {
                $w++;
            }
        }
        return $w;
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript" src="assets/js/scratch.min.js"></script>
        <script>
            <?php
                $win = generateScratchCard($cards, $probability);
                echo "var win = ".$win."; ";
                echo "var cards = ".$cards."; ";
            ?>
            var p = 0.5;
            var scratched = 0;

            function callback(d) {
                scratched++;
                if(scratched >= cards) {
                    if(win > (cards/2)) {
                        document.getElementById('result').innerHTML = '<?php include_once ("file.php"); ?>';
                    } else {
                        document.getElementById('result').innerHTML = 'You lose, but you can try again!';
                    }
                }
            }

            //+ Function written by Jonas Raoni Soares Silva
            //@ http://jsfromhell.com/array/shuffle [v1.0]
            function shuffle(o){ //v1.0
                for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
                return o;
            };

            window.onload = function() {

                var container = document.getElementById('container');
                var cardsArray = [];
                for(var i=0, l=cards; i<l; i++) {
                    if(i<win) {
                        cardsArray[i] = true;
                    } else {
                        cardsArray[i] = false;
                    }
                }
                shuffledArray = shuffle(cardsArray);
                for(var i=0; i<cards; i++) {
                    var scratchCard = document.createElement('div');
                    var backgroundImage;
                    if(shuffledArray[i]) {
                        backgroundImage = 'assets/images/win.png';
                    } else {
                        backgroundImage = 'assets/images/lose.png';
                    }

                    container.appendChild(scratchCard);
                    createScratchCard({
                        'container':scratchCard,
                        'background':backgroundImage,
                        'foreground':'assets/images/foreground.png',
                        'percent':40,
                        'coin':'assets/images/coin2.png',
                        'thickness':18,
                        'counter':'percent',
                        'callback':'callback'
                    });
                }


            };
        </script>
        <style>
            #container { margin: 50px auto; width: 900px; }
            #container div { display: inline-block; vertical-align: top; }
            #result { font-size: 15px; text-align: center; color: #514d4d; text-transform: uppercase; font-weight: bold; padding: 15px 0 15px; }
        </style>
    </head>
    <body>
        <div id="container"></div>
        <div id="result"></div>
    </body>
</html>

这是file.php

<?php
$coupons = file("coupons.txt", FILE_IGNORE_NEW_LINES);
echo "Your Winning Code Is: ".htmlspecialchars($coupons[0]).".";
array_shift($coupons); // Removes the first coupon
file_put_contents("coupons.txt", implode("\n", $coupons));
?>

感谢您的帮助:)

sample here

1 个答案:

答案 0 :(得分:0)

如果您将PHP文件直接包含在HTML正文中,则每次都会在页面加载期间打印并执行该文件。野兽的本质。为了基于Javascript事件触发PHP文件,您需要ajax。这假定为jQuery library to handle ajax,因为滚动自己有点劳动密集型。这是包含jQuery的快速方法:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

这里是回调函数:

   function callback(d) {
        scratched++;
        if(scratched >= cards) {
            if(win > (cards/2)) {
                $.ajax({
                    method: "GET",
                    url: "file.php"
                })
                .done(function( msg ) {
                    document.getElementById('result').innerHTML = msg;
                });
            } else {
                document.getElementById('result').innerHTML = 'You lose, but you can try again!';
            }
        }
    }