user2581346
user2581346

Reputation:

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

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.

Upvotes: 0

Views: 7020

Answers (3)

Alex Hill
Alex Hill

Reputation: 711

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.

Upvotes: 0

MattLoszak
MattLoszak

Reputation: 585

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!

Upvotes: 3

Zorayr
Zorayr

Reputation: 24922

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 components can communicate 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.com/?variable=value and access it from your PHP script using, $_GET['variable'].

Hope this helps.

Upvotes: 3

Related Questions