简体   繁体   中英

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. So here is my situation: I'm creating a simple game in HTML using JS and CSS. All you have to do is to find 6 differences between two images. There is Index.html (like parent window) and JS functions changing images in div of that parent window. There are some audio, some animations with canvas elements... Everything working excellent with 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. User name is entering on game beginning in HTML input and I'm holding that data in Main.js ParentObject.UserName . I have JS function

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

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

<?php

if(isset($_POST['q']))
  echo $_POST['q'];
 else
  echo "Not posted";
?> 

Then check the output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM