transferring php form data to a js window

2019-08-27 08:19发布

问题:

I am designing a game with javascript and jquery. In my game when a player talks to another character it opens the text as an external file using the window.open('') function. At the beginning of the game the user has to enter a name for their character it is then processed by php:
<form action="play.php" method="get">
<input type="text" name="name"/><input type="submit" value="confirm"/>
</form>
.
As I said before when a player talks to another player it opens the external file but what I am asking is how to transfer the form data to the external window opened with window.open('') I have already tried
function txtone()
{
window.open('txtone.php?name=<?php echo $_GET["name']; ?>')
}

but it didn't work. So basically what I want in the end is an external window opened with window.open('txtone.php') that can receive the php data from the page it was opened from
(rather than opening txtone.php it would open txtone.php?name=example).

Edit: I have an idea I could send a php variable to the child window opened with window.open but I have no idea how to do this mabye with window.opener?

回答1:

Did you try window.open('txtone.php?name=<?php echo htmlspecialchars($_GET["name"]); ?>')



回答2:

function txtone() {
    window.open('txtone.php?name=<?= $_GET["name"] ?>');
}


回答3:

When the user "logs in" - so to say - with his user name at the beginning of your game, maybe try to save the name in a Cookie and read it when he wants to talk to others and give his name to window.open with JS.

window.open('txtone.php<?php echo $_GET["name'}; ?>')

^That line has a typo at the end (} instead ]) and would also lead to a result of:

txtone.phpSomeUserName

You're missing the parameter there (if that's in your code like that, most likely your culprit). Probably you intended to have something like

txtone.php?username=<?php echo "someUsername"; ?>


回答4:

Your code is a bit off

Change

window.open('txtone.php<?php echo $_GET["name'}; ?>')

To

window.open('txtone.php<?php echo $_GET['name']; ?>')

Also make sure you're passing a value to your name input