I am making a pop up window with JavaScript using window.open()
. The page to be opened should be stored in a variable.
That variable's name is determined using a PHP <form>
. When the user clicks "confirm", it brings them to play.php?name=userinput
, and when the window.open
function is executed, it currently opens txtone.php
. But I want it to open txtone.php?name=userinput
, including that argument.
I can retrieve the value from the parent window (name=userinput
), using a PHP variable, but I need to transform it into a JavaScript variable, and then use window.open(name);
(The variable's name is name
).
So I am basically asking how do I convert a PHP variable into a JavaScript variable.
I am making a pop up window with JavaScript using window.open()
. The page to be opened should be stored in a variable.
That variable's name is determined using a PHP <form>
. When the user clicks "confirm", it brings them to play.php?name=userinput
, and when the window.open
function is executed, it currently opens txtone.php
. But I want it to open txtone.php?name=userinput
, including that argument.
I can retrieve the value from the parent window (name=userinput
), using a PHP variable, but I need to transform it into a JavaScript variable, and then use window.open(name);
(The variable's name is name
).
So I am basically asking how do I convert a PHP variable into a JavaScript variable.
Share Improve this question edited Dec 28, 2019 at 19:46 trincot 350k36 gold badges271 silver badges322 bronze badges asked Oct 21, 2013 at 14:43 user2872498user2872498 691 gold badge2 silver badges6 bronze badges 3- 5 You could make a pause, every once in a while – Damien Pirsy Commented Oct 21, 2013 at 14:44
- 3 Use sentences in your question. Include whitespace. – allen213 Commented Oct 21, 2013 at 14:45
- possible duplicate of another of OPs questions: transferring php form data to a js window – Jamie Taylor Commented Oct 21, 2013 at 14:46
3 Answers
Reset to default 12Making a PHP variable available in JavaScript happens when the page is generated. For example, assume you have a php variable called $name and you want a JavaScript variable called name. You normally define your JavaScript variable as:
var name='some value';
Instead, you will insert PHP:
var name='<?php echo $name; ?>';
Now, the value of $name in your PHP is also the value of name in your JavaScript at the moment that the page is dynamically created.
UPDATE: When I answered this, I should have explained further...
While the JavaScript and PHP variables have the same value, they are NOT the same variable. If you update $name in your PHP after your print the value to the JavaScript source code, it will not alter the JavaScript value. Similarly, if you change the value of the JavaScript variable, it will not change the value of the PHP variable. In other words, they are two completely separate and unrelated variables. The code above simply copies the value from PHP to JavaScript.
Try this :
where spge
is variable in js
var spge = '<?php echo $status ;?>';
//suppose
<?php $tochange="from php to js" ?>
some where
//Head
<script>
function change(){
var temp = document.getElementById("change").innerHTML;
alert ("temp");
}
</script>
//body
<body onload="change()" >
//create a invisible tag in body php document
<span id="change"><?php echo $tochange ?></span>
.....