1

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.

3
  • 5
    You could make a pause, every once in a while Commented Oct 21, 2013 at 14:44
  • 3
    Use sentences in your question. Include whitespace. Commented Oct 21, 2013 at 14:45
  • possible duplicate of another of OPs questions: transferring php form data to a js window Commented Oct 21, 2013 at 14:46

3 Answers 3

11

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

Sign up to request clarification or add additional context in comments.

Comments

4

Try this :

where spge is variable in js

var spge = '<?php echo $status ;?>';

Comments

1

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


.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.