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 :(

pzztzz
  • 181
  • 3
  • 8
  • 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 – mishu Nov 22 '11 at 12:39
  • thanks a lot... I'm 0 at ajax :) – pzztzz Nov 22 '11 at 12:41
  • 1
    possible duplicate of [Access a JavaScript variable from PHP](http://stackoverflow.com/questions/2338942/access-a-javascript-variable-from-php) – mario Nov 22 '11 at 12:41
  • From PHP to JS yes, other way just with Ajax. –  Nov 22 '11 at 12:43

7 Answers7

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
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";
}
Till Helge
  • 9,253
  • 2
  • 40
  • 56
  • 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. – Till Helge Nov 22 '11 at 12:39
  • Possible with AJAX, see my answer for example using jQuery – Shadow The GPT Wizard Nov 22 '11 at 12:40
1

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

SureshKumar Vegesna
  • 1,192
  • 5
  • 19
  • 37
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

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.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
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.

David
  • 209
  • 1
  • 6
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>
Sonal Khunt
  • 1,876
  • 12
  • 20