douou0977 2016-08-25 13:25
浏览 14
已采纳

PHP和JavaScript传输数据的最有效方法是什么? [关闭]

I've seen many different approaches for transferring javaScript and PHP data, but I question the efficiency and precision. What I mean by that is, say for example you have a javaScript platformer (game) that has 10 levels. You have many users with each of them having/owning a different account. You want to make this game, so that when a user closes the browser tab or saves and wants to play it later; he/she doesn't have to restart from level 1. Instead you want it so that when the user saves, you store the current level number that the user saved on, in a mySQL database in his or her account data. Now, the only problem being, is that the number of levels the user completed, is stored in a javaScript variable, but you need to somehow transfer/get that variable in PHP in order to do so, how would you do that? I've seen document.write (<set php variable here>), but I don't think that is the most efficient way to approach this. Now, I question the efficiency only after I figure out a different way to do this simply for the reason that the current method I use lacks more involved back-to-back computation. So, generally speaking, are there any other more effective solutions that are more flexible?

  • 写回答

1条回答 默认 最新

  • doucao1066 2016-08-25 13:31
    关注

    My approach would be to save the finished level via an AJAX call after the user has successfully finished a level. That way you dont have to worry about the user closing the browser, ie if he does and he has not finished the level then he must repeat the last level he did not finish.

    EDIT:

    JS script:

    $.ajax({
      method: "POST",
      url: "update_finished_level.php",
      data: { user_id: "1", level_finished: "4", some_other_parameter: "some value" }
    })
    .done(function( msg ) {
        if(msg == 1) {
            // OK
        } else {
            // SOMETHING WENT WRONG
        }
    });
    

    PHP script (suppose you are using mysqli to connect to your DB):

    $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    
    if ($link->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error);
        exit();
    }
    
    if($mysqli->query("UPDATE user SET level_finished = " . $_POST['level_finished'] . " WHERE user_id = " . $_POST['user_id']) === TRUE) {
        echo 1;
    } else {
        echo 0;
    }
    

    BEWARE: You have to escape any output that you get from a user so at least use the mysql_real_escape_string function, which BTW isnt safe anymore.

    I would suggest you use something like the PHP PDO library: http://php.net/manual/en/book.pdo.php. There are many tutorials out there how to use it with prepared statements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大