javascript - 使用隐藏形式将 JavaScript 分数从游戏发送到 PHP - 我没有得到任何值?

标签 javascript php html

您好,我想将游戏(JS)的分数发送到 php,以便稍后将其存储在我的数据库中。但是当我单击提交按钮保存所有分数时,我没有得到任何值。我正在使用隐藏表单。

正如你所看到的,我有一个包含 xWon、xLose 等的表格...当我玩游戏时,一切正常,并且 JS 的分数会发布在该表格中。但是当我点击提交按钮时,当我在代码顶部使用 echo 时,我没有得到任何值..? 这是 HTML 代码

<?php
if(isset($_POST['xWon_score']) && isset($_POST['xLose_score']) && 
isset($_POST['drawGame_score']) && isset($_POST['totalGames_score']))
{

$xWon_score = $_POST['xWon_score'];
$xLose_score = $_POST['xLose_score'];
$drawGame_score = $_POST['drawGame_score'];
$totalGames_score = $_POST['totalGames_score'];


echo $xWon_score . $xLose_score . $drawGame_score . $totalGames_score;
}else
echo "Testing......";

?>


<!Doctype html>
<html>
<head>
<script src="scripts/game.js"></script>
<link type="text/css" rel="stylesheet" href="styles/general.css">
</head>

<body onload="startGame();">

    <table border="1" align="center">
    <tr>
        <td>X won</td>
        <td>X lose</td>
        <td>Draw</td>
        <td>Total games</td>
    </tr>
    <tr>
        <td id="xWon_score">0</td>
        <td id="xLose_score">0</td>
        <td id="drawGame_score">0</td>
        <td id="totalGames_score">0</td>
    </tr>
    </table>
    </div>


    <div id="button_div">
    <br>
    <button id="button" type="button" onclick="alert('Restarting game')"><a href="javascript:startGame();">New Game</a></button>

<!-- Problem section ????? -->  

    <form method="post" action="game.php">  
        <input type="hidden" value="" id="Won_score" name="xWon_score"/>
        <input type="hidden" value="" id="Lose_score" name="xLose_score"/>
        <input type="hidden" value="" id="draw_score" name="drawGame_score"/>
        <input type="hidden" value="" id="total_score" name="totalGames_score"/>

        <!-- Save highscore button -->
        <button type="submit" onclick="alert('Saving highscore')">Save highscore</button>

        </form>

下面是打印 JS 分数的函数:

    function recordWin(winner){
                    if (winner == "X"){
                        xWon++;
                        totalGames++;
                    }else if (winner == "O"){
                        xLose++;
                        totalGames++;
                    }else if (winner ==""){
                        drawGame++;
                        totalGames++;
                    }saveStats();
                }

                function saveStats(recordWin){

                    document.getElementById("xWon_score").innerText = xWon;
                    document.getElementById("xLose_score").innerText = xLose;
                    document.getElementById("drawGame_score").innerText = drawGame;
                    document.getElementById("totalGames_score").innerText = totalGames;

    // Somthing Wrong here?
document.getElementById("Won_score").value = xWon;
document.getElementById("Lose_score").value = xLose;
document.getElementById("draw_score").value = drawGame;
document.getElementById("total_score").value = totalGames;
}

最佳答案

正确的语法是这样的:

document.getElementById("myInput").value;

http://www.w3schools.com/jsref/prop_hidden_value.asp

 document.getElementById("Won_score").value = xWon;
 document.getElementById("Lose_score").value = xLose;
 document.getElementById("draw_score").value = drawGame;
 document.getElementById("total_score").value = totalGames;

关于javascript - 使用隐藏形式将 JavaScript 分数从游戏发送到 PHP - 我没有得到任何值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30241904/

相关文章:

html - 如何将 <div> 定位在另一个 <div> 的正下方

html - <center> 标签会从 HTML 中逐步淘汰吗?

javascript - 需要在用户点击图片时进行 POST

javascript - 如何访问 Angular Material 表特定行中的模板变量引用? ( Angular 7)

javascript - 将图像(或位图)转换为二维矩阵

javascript - JS - 如何获取按钮文本 "end"位置

javascript - 具有特定延迟的 100 个动画循环

javascript - 为什么我的 CSS 定位在 Mac OS 上看起来不同

php - 如何创建可编辑的下拉列表?使用 AJAX

php - Heredoc 在 JavaScript 中转义 PHP 变量