如何将实时javascript变量转换为php变量?

时间:2017-08-05 01:04:12

标签: javascript php html

我对此非常陌生,所以请原谅我的意大利面条代码 - 我正在尝试制作一个在游戏过程中实时跟踪篮球统计数据的网页,然后使用php保存总统计数据。现在,我只需按一下按钮就可以将从html页面实时更新的变量传递给php。我很确定我甚至不接近,但是在尝试这个时我得到了'未定义的索引'消息。这是我的html页面:

this

我的php代码:

<head>
  <meta charset="utf-8">
  <title>Scoring</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
    var points = 0;
    var assists = 0;
    var rebounds = 0;
    function add1point(){
      points++;
      document.getElementById('displaypoints').innerHTML = '<p>Points: ' + points;
    }
    function add2points(){
      points = points + 2;
      document.getElementById('displaypoints').innerHTML = '<p>Points: ' + points;
    }
    function add3points(){
      points = points + 3;
      document.getElementById('displaypoints').innerHTML = '<p>Points: ' + points;
    }
    function add1assist(){
      assists++;
      document.getElementById('displayassists').innerHTML = '<p>Assists: ' + assists;
    }
    function add1rebound(){
      rebounds++;
      document.getElementById('displayrebounds').innerHTML = '<p>Rebounds: ' + rebounds;
    }
  </script>
  </head>
<body>
  <center>
    <br>
    <button onclick="add1point()">+1 Point (Made Free-Throw)</button>
    <br>
    <br>
    <button onclick="add2points()">+2 Points (Made Field-Goal)</button>
    <br>
    <br>
    <button onclick="add3points()">+3 Points (Made Three-Pointer)</button>
    <br>
    <br>
    <br>
    <button onclick="add1assist()">+1 Assist</button>
    <br>
    <br>
    <br>
    <button onclick="add1rebound()">+1 (Offensive) Rebound</button>
    <br>
    <br>
    <button onclick="add1rebound()">+1 (Defensive) Rebound</button>
    <br>
    <br>
    <br>
    <br>
<form method="post" attribute="post" action="scoring.php">
    <div id="displaypoints"><script type="text/javascript">document.write('<p>Points: ' + points);</script></div>
    <div id="displayassists"><script type="text/javascript">document.write('<p>Assists: ' + assists);</script></div>
    <div id="displayrebounds"><script type="text/javascript">document.write('<p>Rebounds: ' + rebounds);</script></div>
    <br>
    <br>
    <br>
    <input type="submit" name="finish" id="finish" value="Finish Game">
</button>
</form>
  </center>
</body>
</html>

任何帮助都将非常感谢:)

2 个答案:

答案 0 :(得分:1)

正如詹姆斯的评论所说,你可以通过表格中的输入轻松完成。我想你不希望用户在游戏结束时改变值,所以你可以使用隐藏的输入,如下所示:

<form method="post" action="scoring.php">
    <div id="displaypoints"><script type="text/javascript">document.write('<p>Points: ' + points+'</p><input type="hidden" name="points" value="'+points+'">');</script></div>
    ...
<input type="submit" name="finish" id="finish" value="Finish Game">

答案 1 :(得分:1)

我重写了代码的某些部分。我希望你不要介意:)。

request -> Controller -> Service -> Repository -> Entity (DB representation)
response -> Repository return a Entity -> Service convert Entity to a DTO -> Controller