0

how can use JS variable in PHP like this ?

<script>
x = document.getElementById(1).value;
var listOf = <?php echo $listOf[x]; ?>;
</script>

this doesn't work :(

asked Nov 22, 2011 at 12:35
4
  • you can't do that.. php is a server side language and js is a client scripting language.. at the moment the js script runs the php parsing is over.. in your sample you are assigning at run time to the js var listOf the value from the array $listOf on a key equal to the constant x.. depending on what you need ajax might be the answer Commented Nov 22, 2011 at 12:39
  • thanks a lot... I'm 0 at ajax :) Commented Nov 22, 2011 at 12:41
  • 1
    possible duplicate of Access a JavaScript variable from PHP Commented Nov 22, 2011 at 12:41
  • From PHP to JS yes, other way just with Ajax. Commented Nov 22, 2011 at 12:43

7 Answers 7

1

And rightfully so. PHP is executed on the server, while JavaScript is executed in the client's browser. Those are two different contexts and the variables from one are not visible in the second.

You need to have your PHP script output a JavaScript version of the array and then use this one in your script. Something like this:

<?php
echo "listArray = new Array();\n";
foreach ($listArray as $key => $value) {
 echo 'listArray[', $key, '] = ', $value, ";\n";
}
answered Nov 22, 2011 at 12:37
Sign up to request clarification or add additional context in comments.

2 Comments

I added something to my answer. You can output your PHP array as JavaScript array and thus make it available from JavaScript. But you have to realize that it's a copy...it's not the same. Another solution might be AJAX as @ShadowWizard mentioned in his answer below.
Possible with AJAX, see my answer for example using jQuery
1

You can't use it directly like this.

You'll have to use AJAX to send the value from client side to server side and only then PHP can see it.

Using jQuery it can become really simple though:

x = document.getElementById(1).value;
$.get("mypage.php?x=" + x, function(result) {
 alert("response from PHP: " + result);
});

And in the PHP read the x from querystring and send proper output.

answered Nov 22, 2011 at 12:36

Comments

1

it is not possible because both are different languages so you cant use javascript varible in php inside javascript

answered Nov 22, 2011 at 12:47

Comments

0

This is not possible. PHP is executed serverside, even before its output reaches your browser. JS is executed clientside, in the browser. You can't do this, unless you call some other PHP script with this variable x.

answered Nov 22, 2011 at 12:39

Comments

0

You can't. JavaScript is run in your browser while PHP gets run on the server. The only way for JavaScript to communicate with PHP is by using AJAX (XMLHttpRequest), separating the JavaScript from the PHP.

answered Nov 22, 2011 at 12:39

Comments

0

like this if you put php in any single or double quotes than it work

<script type='text/javascript'>
x = document.getElementById(1).value;
var listOf = "<?php echo $listOf[x]; ?>";
</script>
answered Nov 22, 2011 at 13:29

Comments

0

This will convert javascript variable to php variable.

<script>
function sud(){
javavar=document.getElementById("text").value; 
document.getElementById("rslt").innerHTML="<?php 
$phpvar='"+javavar+"'; 
echo $phpvar.$phpvar;?>";
}
function sud2(){
document.getElementById("rslt2").innerHTML="<?php 
echo $phpvar;?>";
}
</script> 
<body>
<div id="rslt">
</div>
<div id="rslt2">
</div>
<input type="text" id="text" />
<button onClick="sud()" >Convert</button>
<button onClick="sud2()">Once Again</button>
</body>

Demo: http://ibence.com/new.php

answered Mar 15, 2013 at 18:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.