简体   繁体   中英

function variable not passing to global variable

Good day all, I've two pages of php file and an external javascript file. I want to pass a selected radio button's value to a jquery global variable so that I can view the div element which has the same id as selected radio button's value. Whenever I click PLAY! button I don't see my div element on the next page. Here are my codes:

player-choose.php script:

<head>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/mycustom.js"></script>
</head>

<body>
<div id="player-list">
    <input type="radio" name="player" value="fighter" id="fighter-radio"><label for="fighter-radio"><img src="images/heroes/fighter-01.png" width="74" height="70"></label>
    <input type="radio" name="player" value="pakhi" id="pakhi-radio"><label for="pakhi-radio"><img src="images/heroes/pakhi.png" width="95" height="70"></label>
</div>
<button id="play">PLAY!</button>
</body>

mycustom.js script:

var playerID;

function start(){
spawnhero();
}

$(function(){
$("#play").click(function(){     
    window.location.href = 'index.php';
    playerID = $('input[name=player]:checked').val();

});
})

function spawnhero () {
$("#content").append($("<div>").attr('id', playerID));
}

index.php script:

<head>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/mycustom.js"></script>
</head>

<body onload="start()">
<div id="content">
<div id="galaxy"></div> 
</div>
</body>

It's a very simple thing but I don't know why it's not working. Am I doing something wrong here? Please if anyone finds a solution enlighten me. Tnx!

global variables are not persistent across pages. Once you load your index.php , it will have the new global scope(window variable).

I suggest passing a parameter.

$("#play").click(function(){
    playerID = $('input[name=player]:checked').val();    
    window.location.href = 'index.php?id=' + playerID;

});

afterward, inside your index.php script , read the parameter and assign accordingly.

If you're moving to a new page (window.location = ...), you'll need some slightly more complicated way of transferring information between those pages - for the most part, HTTP/HTML is "stateless", with the exception of technologies like cookies. JavaScript variables get wiped out entirely - it's actually re-parsing the entire JQuery library on each new page (not to say that's something to avoid)

For a video game, as long as player information doesn't include server components (I could be wrong) my recommendation would be saving player information in sessionStorage .

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

However, if this is a server-based game in which your choice of player matters beyond the local computer, you'd likely want to send the player ID to the server, either by structuring the page request differently:

window.location.href = 'index.php?playerId=' + playerId;

Or by POST ing the data as a form; most easily accomplished by structuring your submit button as an <input type="submit"> , and wrapping all your <input> elements in a <form method="POST"> object.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

From there, your server software could write the second page's response out differently based on the given information - you can even customize what JavaScript is written inside of a <script> tag using PHP directives.

var playerId = "<?php print($_POST['playerId']); ?>";

Hopefully that helps get you started.

Alternative solution is you could you use JavaScript or jQuery cookie or localstorage. You can get/set values across page loads/redirects but these are not passed to server.

jQuery Cookie

var playerID = $('input[name=player]:checked').val();
$.cookie("playerId", playerID);

LocalStorage

var playerID = $('input[name=player]:checked').val();
localStorage.setItem("playerId", playerID);

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