Jump to content

Archived

This topic is now archived and is closed to further replies.

baho

javascript variable to php variable

Recommended Posts

How can i pass the javascript variable 'i' value to php variable '$k'?

<html><body>
<script language="javascript">
var i=5;
<?php $k;?>
</script>
</body></html>

Thanx.

Share this post


Link to post
Share on other sites
[code]
<html><body>
<script language="javascript">
var $i=5;
<?php $k = $i;?>
</script>
</body></html>[/code]
when you create the javascript variable, make it readable as a variable by PHP by using the PHP standard method of giving it a $ at the beginning.

Share this post


Link to post
Share on other sites
thanx, but this code doesn't work

[color=green]<script language=javascript>
  $k=5;
  alert ($k);
  alert ("<?php echo $k?>");
  <?php $i=$k;?>
  alert ("<?php echo $i?>");
</script>[/color]

Can you tell me what is the problem here?

Share this post


Link to post
Share on other sites
PHP code is processed by the server before the browser processes your javascript.  Set your variables first in PHP.

[color=orange]<script language="javascript">[/color]
[color=orange]var [/color]<?php $k=5; echo('[color=orange]$k=[/color]'.$k); ?>[color=green];[/color]
[color=orange]alert($k);[/color]
[color=orange]alert("[/color]<?php echo($k); ?>[color=orange]");[/color]
<?php $i=$k; ?>
[color=orange]alert("[/color]<?php echo $i ?>"[color=orange]);[/color]
[color=orange]</script>[/color]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.