Pass from php to javascript. How to pass a variable from JavaScript to PHP

It just so happens that due to the nature of my work I have to answer both complex technical issues, and frankly noobish. What to do, the level of training of different specialists is different.

They asked: “How to pass a variable from Javascript to PHP.” I would like to start the answer to this question with the fact that Javascript and PHP run on different physical machines, which means that simply “passing a variable”, in the usual understanding of this process, will not work.

Since Javascript runs on client machine(client), and PHP on the server side (server), then let’s call the data transfer between Javascript and PHP the term “client-server exchange”, it will be more correct.

On the web, data exchange is carried out according to the following scheme: a request is sent to the server from the client, this request is processed by the server, after which it returns some data (response) to the client. This method of exchange is also used by the browser itself. The URL that you enter into the address bar is the request. And the response is HTML, which is displayed on the browser page.

However, it is not necessarily the browser itself that must request data from the server. Your Javascript code that runs on the page can also act as a client. To do this, they use the built-in browser component XMLHttpRequest, which itself can make requests to the server and receive responses from it, while the HTML page itself is not updated or changed at all.

Working with the server through an XMLHttpRequest object is often called more popular word- AJAX.

However, I personally do not recommend working directly with this object. The point is that on different browsers it works slightly differently and this will need to be taken into account in the code. Also, "pure" Javascript does not have convenient means working with events, so you will end up with a decent “footcloth” of code, which will also most likely be “buggy”.

The code using this library is quite simple and concise. for example, if you need to pass the value v=6789 to the server, then you can run the following code

JQuery.get("/index.php?v=6789", function(result)( alert(result); ));

In this case, your script at /index.php will receive the value in the $_GET[‘v’] variable. And everything that it outputs to the output stream (for example, using echo) will end up in the result variable and will be output to the alert() pop-up dialog.

As you can see, everything is simple.

You can similarly send data via POST, as well as send data encoded in JSON (the most commonly used method), but I recommend that you read about this yourself in the jQuery documentation (see methods .ajax(), .post()).

8 answers

HTML/HTTP is stateless, in other words, what you did/saw on previous page, completely unrelated current page. Except when you're using something like sessions, cookies, or GET/POST. Sessions and cookies are quite easy to use, and the session is much more secure than cookies. More secure, but not completely secure.

session:

//On page 1 $_SESSION["varname"] = $var_value; //On page 2 $var_value = $_SESSION["varname"];

Don't forget to run session_start(); on both of these pages, before attempting to access the $_SESSION array, and also before any output is sent to the browser.

//One page 1 $_COOKIE["varname"] = $var_value; //On page 2 $var_value = $_COOKIE["varname"];

A big difference between sessions and cookies is that the value of the variable will be stored on the server if you are using sessions and on the client if you are using cookies. I can't think of any good reason to use cookies instead of sessions, unless you want the data to persist between sessions, but even then it might be better to store it in the DB and retrieve it based on the username or ID.

GET and POST