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.

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

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用