Jump to content

javascript variable to php variable


baho

Recommended Posts

[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.
Link to comment
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?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.