admin管理员组

文章数量:1336207

How to capture a Javascript function's return value into a PHP variable?

Example:

Javascript: javascriptFunction { return null;};

PHP: $php_variable = javascriptFunction();

If clarification is needed, please let me know.

How to capture a Javascript function's return value into a PHP variable?

Example:

Javascript: javascriptFunction { return null;};

PHP: $php_variable = javascriptFunction();

If clarification is needed, please let me know.

Share Improve this question asked Aug 17, 2013 at 1:52 user2581346user2581346 1
  • Yes... what is it exactly that you're trying to do? – jeremy Commented Aug 17, 2013 at 1:53
Add a ment  | 

3 Answers 3

Reset to default 3

JavaScript runs on the client side, while PHP runs on the server side. As such, you cannot execute JavaScript code inside your PHP code; however, these two ponents can municate via several channels including:

  1. HTTP Cookies
  2. AJAX
  3. WebSockets

The easiest approach would be to set a query parameter on your URL: example./?variable=value and access it from your PHP script using, $_GET['variable'].

Hope this helps.

Without any idea of what you're trying to do...

Use jquery (with ajax):

$.get("test.php", { name: "John", time: "2pm" } );

Where test is the php file you're sending data to, and John and 2pm are the data.

Then in test.php do:

$name = $_GET['name']

and do the same for time!

You would have to AJAX the value to the server and pull it from $_GET or $_POST:

$.ajax({type:"get", data:javascriptFunction(), url:script.php });

Then, simply use $value = $_GET.

本文标签: How to capture a Javascript function39s return value into a PHP variableStack Overflow