-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();

6
  • and why you want to do this? Commented Nov 1, 2013 at 6:17
  • PHP runs on the server. First. It generates HTML/JavaScript which is run on the client. Later. To pass data back to PHP you need to send it to the server (form post, AJAX, SignalR, etc). Commented Nov 1, 2013 at 6:17
  • stackoverflow.com/questions/1917576/… Commented Nov 1, 2013 at 6:20
  • I get you @user2864740 my mistake Commented Nov 1, 2013 at 6:20
  • You need to use Ajax to pass javascript value into PHP. Commented Nov 1, 2013 at 6:22

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.