如何将实时javascript变量转换为php变量?(How to convert live javascript variables to php variables?)

编程入门 行业动态 更新时间:2024-10-28 16:16:25
如何将实时javascript变量转换为php变量?(How to convert live javascript variables to php variables?)

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

<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>

我的PHP代码:

<!doctype html> <html> <head> <meta charset="utf-8"> <title>Game Finished</title> </head> <body> <?php $points = $_POST['points']; $assists= $_POST['assists']; $rebounds = $_POST["rebounds"]; ?> </p> </body>

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

I'm extremely new to this so please excuse my spaghetti code - I'm trying to make a webpage that keeps track of basketball statistics live during a game, and then saves the total statistics using php afterwards. For now, I just need to pass the variable that is being live updated from my html page to php at the press of a button. I'm pretty sure I'm not even close, but am getting the 'undefined index' message when trying this. Here is my html page:

<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>

And my php code:

<!doctype html> <html> <head> <meta charset="utf-8"> <title>Game Finished</title> </head> <body> <?php $points = $_POST['points']; $assists= $_POST['assists']; $rebounds = $_POST["rebounds"]; ?> </p> </body>

Any help at all would be greatly appreciated :)

最满意答案

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

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Scoring</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> </head> <body> <center> <br> <button onclick="addPoints(1)">+1 Point (Made Free-Throw)</button> <br> <br> <button onclick="addPoints(2)">+2 Points (Made Field-Goal)</button> <br> <br> <button onclick="addPoints(3)">+3 Points (Made Three-Pointer)</button> <br> <br> <br> <button onclick="addAssists(1)">+1 Assist</button> <br> <br> <br> <button onclick="addRebounds(1)">+1 (Offensive) Rebound</button> <br> <br> <button onclick="addRebounds(1)">+1 (Defensive) Rebound</button> <br> <br> <br> <br> <form method="post" attribute="post" action="scoring.php"> <p>Points: <span id="displaypoints"></span></p> <p>Assists: <span id="displayassists"></span></p> <p>Rebounds: <span id="displayrebounds"></span></p> <!-- Any input element with "name" attribute will be sent to server (scoring.php script). --> <input type="hidden" name="points" id="points" /> <!-- Any input element with "name" attribute will be sent to server (scoring.php script). --> <input type="hidden" name="assists" id="assists" /> <!-- Any input element with "name" attribute will be sent to server (scoring.php script). --> <input type="hidden" name="rebounds" id="rebounds" /> <br> <br> <br> <input type="submit" name="finish" id="finish" value="Finish Game"> </form> </center> <script type="text/javascript"> // Initial values var points = 0; var assists = 0; var rebounds = 0; // Find "span" element with "displaypoints" id. $displayPoints = $("#displaypoints"); // Set element text to initial points value. $displayPoints.text(points); // Find "span" element with "displayassists" id. $displayAssists = $("#displayassists"), // Set element text to initial assists value. $displayAssists.text(assists); // Find "span" element with "displayrebounds" id. $displayRebounds = $("#displayrebounds"); // Set element text to initial rebounds value. $displayRebounds.text(rebounds); // Function that receives the amount of points. // 1. Adds received amount of points to current amount of points. // 2. Sets the corresponding element text to current amount of points. // 3. Sets the element that's going to be sent to server value to current amount of points. function addPoints(amount){ points += amount; $displayPoints.text(points); $("#points").val(points); } // Function that receives the amount of assists. // 1. Adds received amount of assists to current amount of assists. // 2. Sets the corresponding element text to current amount of assists. // 3. Sets the element that's going to be sent to server value to current amount of assists. function addAssists(amount){ assists += amount; $displayAssists.text(assists); $("#assists").val(assists); } // Function that receives the amount of rebounds. // 1. Adds received amount of rebounds to current amount of rebounds. // 2. Sets the corresponding element text to current amount of rebounds. // 3. Sets the element that's going to be sent to server value to current amount of rebounds. function addRebounds(amount){ rebounds += amount; $displayRebounds.text(rebounds); $("#rebounds").val(rebounds); } </script> </body> </html>

I rewrote some parts of your code. I hope you don't mind :).

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Scoring</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> </head> <body> <center> <br> <button onclick="addPoints(1)">+1 Point (Made Free-Throw)</button> <br> <br> <button onclick="addPoints(2)">+2 Points (Made Field-Goal)</button> <br> <br> <button onclick="addPoints(3)">+3 Points (Made Three-Pointer)</button> <br> <br> <br> <button onclick="addAssists(1)">+1 Assist</button> <br> <br> <br> <button onclick="addRebounds(1)">+1 (Offensive) Rebound</button> <br> <br> <button onclick="addRebounds(1)">+1 (Defensive) Rebound</button> <br> <br> <br> <br> <form method="post" attribute="post" action="scoring.php"> <p>Points: <span id="displaypoints"></span></p> <p>Assists: <span id="displayassists"></span></p> <p>Rebounds: <span id="displayrebounds"></span></p> <!-- Any input element with "name" attribute will be sent to server (scoring.php script). --> <input type="hidden" name="points" id="points" /> <!-- Any input element with "name" attribute will be sent to server (scoring.php script). --> <input type="hidden" name="assists" id="assists" /> <!-- Any input element with "name" attribute will be sent to server (scoring.php script). --> <input type="hidden" name="rebounds" id="rebounds" /> <br> <br> <br> <input type="submit" name="finish" id="finish" value="Finish Game"> </form> </center> <script type="text/javascript"> // Initial values var points = 0; var assists = 0; var rebounds = 0; // Find "span" element with "displaypoints" id. $displayPoints = $("#displaypoints"); // Set element text to initial points value. $displayPoints.text(points); // Find "span" element with "displayassists" id. $displayAssists = $("#displayassists"), // Set element text to initial assists value. $displayAssists.text(assists); // Find "span" element with "displayrebounds" id. $displayRebounds = $("#displayrebounds"); // Set element text to initial rebounds value. $displayRebounds.text(rebounds); // Function that receives the amount of points. // 1. Adds received amount of points to current amount of points. // 2. Sets the corresponding element text to current amount of points. // 3. Sets the element that's going to be sent to server value to current amount of points. function addPoints(amount){ points += amount; $displayPoints.text(points); $("#points").val(points); } // Function that receives the amount of assists. // 1. Adds received amount of assists to current amount of assists. // 2. Sets the corresponding element text to current amount of assists. // 3. Sets the element that's going to be sent to server value to current amount of assists. function addAssists(amount){ assists += amount; $displayAssists.text(assists); $("#assists").val(assists); } // Function that receives the amount of rebounds. // 1. Adds received amount of rebounds to current amount of rebounds. // 2. Sets the corresponding element text to current amount of rebounds. // 3. Sets the element that's going to be sent to server value to current amount of rebounds. function addRebounds(amount){ rebounds += amount; $displayRebounds.text(rebounds); $("#rebounds").val(rebounds); } </script> </body> </html>

更多推荐

本文发布于:2023-08-06 14:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   转换为   如何将   实时   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!