如何触发javascript css动作?

时间:2016-12-06 14:49:32

标签: javascript php jquery html css

我有一个记忆游戏代码,使用javascript,php和css。

我想以某种方式注册该事件,当游戏由php完成时,我可以将结果保存在数据库中。

换句话说,我想将PHP代码放在<div id="player_won"> </div>中并正确触发该获胜事件。

  1. CSS

    #player_won{
    display: none;
    }
    
  2. 的javascript

    $(document).ready(function(){
    $(document).bind("game_won", gameWon);
    }
    
    function gameWon(){
    $.getJSON(document.location.href, {won: 1}, notifiedServerWin);
    var $game_board = $("#game_board");
    var $player_won = $("#player_won");
    $game_board.hide();
    $player_won.show();
    $game_board = $player_won = null;
    };
    

1 个答案:

答案 0 :(得分:1)

您想要创建一个从页面发送一些信息的ajax调用,并告诉下面的php文件,如果玩家赢了或输了。之后,您可以处理foo.php中播放器所需的逻辑,并将Json发送回ajax调用内的成功函数,并相应地更新您的页面。

索引

   $(document).ready(function () {
   //look for some kind of click below
            $(document).on('click', '#SomeId', function () {

       //Get the information you wish to send here
       var foo = "test";
                $.ajax({
                    url: "/foo.php",
                    type: 'POST',
                    cache: false,
                    data: {Info: foo},
                    dataType: 'json',
                    success: function (output, text, error)
                    {

                    //here is where you'll receive the son if successfully sent
                   if(ouput.answer === "yes"){
                        $("#player_won").show();
                    } else { 
                       // Do something else
                    }

                    },
                    error: function (jqXHR, textStatus, errorThrown)
                    {
                       //Error handling for potential issues.
                        alert(textStatus + errorThrown + jqXHR);
                    }
                })
            })
        });

foo.php

<?php

    if(isset($_POST['Info'])){
        //figure out if what was sent is correct here.

        if($_POST['Info'] === "test"){
        $data['answer'] = "yes";

        echo json_encode($data);

        exit;
      } else {
        // do something else

       }
   }
?>