2016-04-24 1 views
1

リフレッシュが実行されるたびに、ファイルから自分の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> 

は、あなたが直接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

答えて

0

を助けてくれてありがとうfile.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!'; 
      } 
     } 
    } 
+0

が働いているように見えたことありがとう:) –

+0

問題ありません。私の答えをアップアップして受け入れることができれば、感謝します。 – larsAnders

関連する問題