简体   繁体   English

如何在刷新时保存变量?

[英]How can I save variables on refresh?

<!DOCTYPE HTML>
<html>


<head>
    <title>Zautra Levels</title>
    <h2 style="font-family: Trebuchet MS; color: blue;"">Zautra Levels</h2>
    <p> </p>
</head>


<body>

<p>Clickables:</p>
<button id="swag" onclick="lmao()">Gain XP</button> <button id="gold" onclick="getgold()">Get Gold</button> <button id="buyupgrade" onclick="buyupp()">Level Up!</button>
<p> </p>
<div id="total">XP: 0</div>
<div id="goldt">Gold: 0</div>
<div id="upgradess">Level: 0</div>
<div id="upcostt">Required XP: 25</div>



<script>
    var clicks = 0; // How many clicks you have
    var upgrades = 0; // How many upgrades you have purchased
    var upcost = 25; // How much the upgrades cost
    var gold = 0; // How much gold you have

    function updateclicks() { // Declares the function that updates the "Zautra Clicks" Text.
        var v=document.getElementById("total");
        v.innerHTML = 'XP: ' + clicks;
    }

        function updategold() { // Declares the function that updates the "Zautra Clicks" Text.
        var g=document.getElementById("goldt");
        g.innerHTML = 'Gold: ' + gold;
    }

    function updateupgradecounter() { // Declares the function that updates the "Upgrades:" Text.
        var y=document.getElementById("upgradess");
        y.innerHTML = 'Level: ' + upgrades;
    }

    function updateupcost() { // Declares the function that updates the "Upgrade Cost:" Text.
        var z=document.getElementById("upcostt");
        z.innerHTML = 'Required XP:' + upcost;
    }

    var x=document.getElementById("swag"); function lmao() { // When you click the "Click for MOAR Zautra's" Button you get a +1 Click.
        clicks+=1;
        updateclicks();
    }

        var j=document.getElementById("gold"); function getgold() { // When you click the "Click for MOAR Zautra's" Button you get a +1 Click.
        gold+=1;
        updategold();
    }

    var c=document.getElementById("buyupgrade"); function buyupp() {
        if (clicks >= upcost) {
            clicks-=upcost
            upgrades+=1
            upcost*=2
            updateclicks();
            updateupgradecounter();
            updateupcost();
        }
        else
        {
            var clicksdif = upcost - clicks;
            confirm("You need " + clicksdif + " more XP to level up.");
        }
    }
</script>
</body>
</html>

This is the code for my game that I am working on. 这是我正在开发的游戏的代码。 I'm trying to add a button, and when you press it, it saves all of the variables. 我试图添加一个按钮,当您按下它时,它将保存所有变量。 If you're level 5 with 26 XP, and 7 gold, you refresh the page, you still have those stats instead of losing them on refresh. 如果您的等级为5,拥有26 XP和7黄金,则刷新页面,您仍然拥有这些统计信息,而不是在刷新时丢失它们。

Please help! 请帮忙!

(And yeah, I do realize that the code is really messed up, but that is a small issue. I'll fix that sooner or later.) (是的,我的确意识到代码确实很混乱,但这是一个小问题。我迟早会解决该问题。)

you could create a cookie in php: 您可以在php中创建一个cookie:

setcookie("NameOfTheCookie",$value,$expireTime)

and $value can be an array of values as well. $value也可以是值数组。

I believe that actually the easiest way, easier than cookies, is to pass the values via the URL. 我认为,实际上,比cookie更简单的方法是通过URL传递值。 Example: 例:

<form action="yourPage.php?gold=$amount&level=$whatlevel&experience=$experience" method="POST">

//Your refresh button here //这里的刷新按钮

</form> 

and then to retrieve those variables when the page reloads, use: gold=$_POST['gold'] 然后在页面重新加载时检索这些变量,请使用:gold = $ _ POST ['gold']

Another option as well is to use the GET method instead of POST. 同样,另一个选择是使用GET方法而不是POST。 Keep in mind that the file extension needs to be php for this code to work. 请记住,文件扩展名必须是php,此代码才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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