-2

I am wondering if there is any way to call a javascrip function from php and store the return value of the function in php variable.

Something like:

<?php
  var p = test("something");
  echo p;
?>
<script>
 function test(elm){
     return elm+" anotherthing";
 }
</script>
kousha
  • 752
  • 2
  • 10
  • 23

2 Answers2

1

You can't. Javascript is executed on the client and PHP on the server. By the time your PHP code runs the Javascript function doesn't even exist yet.

Crono
  • 10,211
  • 6
  • 43
  • 75
1

You cannot call and evaluate a javascript function from php code. You could however make a request (e.g. AJAX) from the javascript test() function to your php code and read the value.

OK11
  • 74
  • 3