First of all, I'm relatively new to this so I'm sorry if I word stuff weird. So let's say I have the variable $x with the value 1 in PHP. I want to get the variable to my JS-Script and modify it and then send the variable x with the modified value back to my PHP document. Note I don't want to send it to a server and I don't want to use AJAX (have yet to learn it). If there's no other way It'd be nice if someone could show me an example of how to do this with AJAX.
Asked
Active
Viewed 44 times
-2
-
4In the same question, you have "...and I don't want to use AJAX" and "...how to do this with AJAX." Could you clarify your intent? – Chris Forrence Jul 11 '16 at 19:09
-
PHP is server side script. Thus, you can easily do this: var JSVar = ; **IF THE JS IS IN THE PHP PAGE**! Then you can use a POST statement in JQuery (as this is tagged in your question) to send it back to a file. Use json_decode on the PHP page after getting the data after using the file_get_contents("php://input"); command. – A.Sharma Jul 11 '16 at 19:12
-
1"I don't want to send it to a server" Then you don't want to send it back to PHP. PHP is running on your server. – Mike Cluck Jul 11 '16 at 19:24
1 Answers
0
You should use AJAX if possible it is more comfortable and with jQuery not hard to use/understand. But if this topic is new for you, you can try to send it via a window.location.href.
I looked for some good examples for you:
To save PHP variables from PHP to Javascript you often just print them. If you're more familar with Javascript and PHP, you should try to work with JSON-Strings. But I think that would be to much for your first try.
Hope that helps.

Sascha
- 615
- 1
- 9
- 20
-
While these links may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jay Blanchard Jul 11 '16 at 20:56