gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:18:32 25 4
gpt4 key购买 nike

您好,我想将游戏(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com