K
K
Kama Python2017-05-31 19:17:54
PHP
Kama Python, 2017-05-31 19:17:54

How to pass js variable value to php variable?

Hello! Tell me how to pass the value of a variable from a js script to a php variable
Tried to do this

$(document).ready(function () {
    ymaps.ready(function () {
        var geolocation = ymaps.geolocation;
        var region = geolocation.region;
        $.ajax({
            url: "1.php",
            type: "POST",
            data: {region:region}
        });
    });
});


but if you try to display region anywhere
$regionprimer = $_POST['region'];
echo $regionprimer;

then I get NULL

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Filippenko, 2017-05-31
@zer0w

Is the letter sent by filling out some form? You can insert the region value into input[type=hidden] on the form, and send data from it to the script that sends the email. And in the script, the variable will already be available in $_POST

N
Nik Gubin, 2017-05-31
@gubin_niko

You sent an Ajax request to a file and that's the end of the story.
Then no matter how much you open the file, it will be so with an empty variable.
You need to send a request to a file in which the variable will be saved, and in another file the SAVED variable will be displayed. How to save? Lots of options: $_SESSION, setcookie(), write to a file or database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question