-4

I have JavaScript function with pass one parameter to

<script type="text/javascript">
function myfun(var1)
{
   var returnVal = "<?php echo get_value(); ?>";
}
</script>

I want to pass JavaScript variable i.e. var1 to get_value();

zzlalani
  • 22,960
  • 16
  • 44
  • 73
Andy Martin
  • 179
  • 1
  • 2
  • 9

2 Answers2

5

You can't do that.

PHP is executed server-side, Javascript client-side. What that means is, the PHP code is executed before the Javascript.

However, you could use AJAX to call a PHP function from Javascript.

You can refer : how to pass the JavaScript variable to the php function

How to pass JavaScript variables to PHP?

Community
  • 1
  • 1
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
0

Try this:

PHP is executed server-side.
Javascript client-side. 
It means, the PHP code is executed before the Javascript.

Here what you can do is to use jquery-ajax.

Inside that call a URL which returns variable you want.
Use that in result of ajax.
  • Thanks
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27