简体   繁体   English

使用JavaScript函数将HTML输入中的用户名保存到XML或TXT文件中的服务器

[英]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.

相关问题 我可以使用 JAVASCRIPT/jQuery 将表单中的输入保存到 HTML 中的 .txt,然后使用它吗? - Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it? 是否可以使用 html 上的输入值保存文本并使用 JavaScript 写入本地 txt 文件? - Is it possible to save texts from input value on html and write on local txt file by using JavaScript? 如何从.txt Javascript导入用户名/密码 - How to import usernames/passwords from .txt Javascript HTML在本地服务器上保存.txt - HTML save .txt at local server 如何使用Javascript保存xml文件? - how to save xml files using Javascript? 如何使用html输入类型文件控件中的javascript将多个文件一一发布到服务器(由于服务器端限制) - How to post multiple files to server one by one (due to server side restriction) using javascript from html input type file control 如何使用JavaScript在.txt文件中保存html页面内容? - How to save html page content in .txt file using JavaScript? 通过JavaScript将详细信息保存在我的服务器(txt文件)上 - Save details on my server(txt file) from JavaScript Javascript 表使用来自多个 txt 文件的数据并显示在 html 文件上 - Javascript table using data from multiple txt files and displaying on html file 如何查找从服务器运行的html文件或不使用javascript? - How to find html files running from server or not using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM