[英]Save UserNames from HTML input, using JavaScript function to server in XML or TXT files
I spend a lot of hour searching on google information about this, but all I was able to find was separate peaces of code examples and explanations... My technical skills in programming are very low, so I'm, asking, could somebody guide me like ABC, step by step, what exactly I must to do. 我花了很多时间在Google上搜索有关此的信息,但是我只能找到代码示例和解释的和平……我的编程技术水平很低,所以我想问一下,有人可以指导吗我喜欢ABC,要逐步做,到底我该怎么做。 So here is my situation: I'm creating a simple game in HTML using JS and CSS.
这就是我的情况:我正在使用JS和CSS在HTML中创建一个简单的游戏。 All you have to do is to find 6 differences between two images.
您要做的就是找到两个图像之间的6个差异。 There is Index.html (like parent window) and JS functions changing images in div of that parent window.
有Index.html(如父窗口)和JS函数可在该父窗口的div中更改图像。 There are some audio, some animations with canvas elements... Everything working excellent with FireFox.
有一些音频,有一些带有画布元素的动画...一切都与FireFox完美配合。 Now All I want to do is to save User names and their time of how long did it take to find all differences, on server and then, when game ends, show all that data in div of parent window.
现在我想要做的就是保存 用户名和它们的根本需要多长时间来找到所有的差异, 在 服务器 ,然后时候 ,当游戏结束时,显示父窗口的DIV所有的数据。 User name is entering on game beginning in HTML input and I'm holding that data in Main.js ParentObject.UserName .
用户名以HTML 输入开始输入游戏,我将数据保存在Main.js ParentObject.UserName中 。 I have JS function
我有JS功能
GameOver(ParentObject.UserName, ParentObject.StartTime)
and I want to save data on server when this function is triggered. 并且我想在触发此功能时将数据保存在服务器上。 What should I use, how to do that.. this is so simple data to save, but I'm stuck... I try some php code, but something I am doing wrong...
我应该用什么,怎么做..这是要保存的这么简单的数据,但是我被卡住了……我尝试了一些php代码,但是我做错了什么……
function GameOver(UserName, StartTime){
var FinishTime = new Date();
var Time = (FinishTime - StartTime);
if (UserName == "" || UserName == " "){
UserName = "Player";
}
alert("Game over\n"+ UserName+", your time is: "+millisToMinutesAndSeconds(Time));
document.getElementById("Container").scrolling= "yes";
SaveUserName(UserName);
}
function SaveUserName(Name){
try{
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST","SaveData.php?q="+Name,true);
xmlhttp.send();
//xmlhttp.open("GET","newfile.txt",false);
//xmlhttp.send();
//document.getElementById("DivContainer").innerHTML=xmlhttp.responseText;
}catch(e){
alert(e);
}
}
PHP file SaveData.php code just to see is it working (but it's not): PHP文件SaveData.php代码只是为了查看它是否有效(但是不行):
<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<note>";
echo "<from>Jani</from>";
echo "<to>Tove</to>";
echo "<message>Remember me this weekend</message>";
echo "</note>";
?>
Uncomment the code 取消注释代码
xmlhttp.open("POST","SaveData.php?q="+Name,true);
xmlhttp.send();
SaveData.php (Remove all the current code and use below code) SaveData.php(删除所有当前代码并使用下面的代码)
<?php
if(isset($_POST['q']))
echo $_POST['q'];
else
echo "Not posted";
?>
Then check the output 然后检查输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.