I'm coding and I need to pass variables from javascript to PHP. I found the solution but it behaves a little different than expected
<script>
var res = navigator.language;
</script>
<?php
$lang = "<script>document.writeln(res);</script>";
$string = file_get_contents("country.json");
if ($string === false) {
// deal with error...
}
$json_a = json_decode($string, true);
if ($json_a === null) {
// deal with error...
}
// echo $lang;
echo var_dump($lang); // On page it does display what I need and that is string(39) "sr-RS"
foreach ($json_a as $countryCodes) {
// echo var_dump($countryCodes["twoLetter"]);
if($lang == $countryCodes["twoLetter"]){
echo "Success";
}
}
?>
This is what I see on my page string(39) "sr-RS". In 39 characters it contains the <script>document.writeln(res);</script>. So I can not compare these two different values since they are different. Do you know how could I convert it or format it?