Pass two variables in the url in javascript

2

I have a website that has 5 different languages and when choosing a language the text of the page changes! For this I did a function:

<script type="text/javascript">
function submitForm() {
    var thelang = document.getElementById('lang').options[document.getElementById('lang').selectedIndex].value;
    window.location.href = window.location.pathname +'?lang=' + thelang;
}
</script><br/>

The problem is that I now have two id different for this page, and I would like to know how I also include id in this function.

For example the url should look something like this: localhost:8080/.../index.php?id=1&&lang=pt , but since I could not pass id to variable, now the pages are no longer translated. id I search via GET

if (isset($_GET['id']) && !empty($_GET['id'])) 
{
    $id=$_GET['id'];
$id=mysqli_real_escape_string($link,$id);
    $query="SELECT * FROM local where local_id='".$id."'";
    $result=mysqli_query($link,$query);

    while ($row=mysqli_fetch_array($result)) {

        if ($row['local_id'] == 1)
        {
            ?> //AQUI FICA O HTML <?php } ?>


The languages are chosen through a select, what is happening is that you can not load anymore, I can only refresh the page manually, changing the URL ...

    
asked by anonymous 15.06.2018 / 14:14

1 answer

1

Use this help function to add / change querystring values.

I tried to comment so you can see what is happening at each step, but if you have any questions, feel free to ask.

function updateQuerystring(whatKey, newValue) {
    var exists = false; // vamos usar para ver se a chave já existe na querystring
    var qs = []; // a futura querystring (é um array, mas vamos tornar num string no fim)
    var __qs = window.location.search || ""; // a querystring actual ou uma string vazia, já deve ser caso nao exista, mas só para o caso..
    var _qs = __qs.substring(1).split("&"); // substring(1) remove o "?" do inicio e split("&") transforma num array, separando a string nos "&"

    for (var idx in _qs) { // por cada combinacao "chave=valor" na querystring
        var _kv = _qs[idx].split("="); // _qs[idx] é algo tipo "chave=valor" entao usamos split("=") para separar novamente
        var _key = _kv[0]; // 0 = chave
        var _value = _kv[1]; // 1 = valor

        if (_key === whatKey) { // se a _key for a que queremos alterar (a que foi passada a esta funcao), alteramos entao o valor
            _value = newValue;
            exists = true;
        }

        // metemos no tal novo array pra querystring, se for encontrado no if acima, entao _value vai ser o novo valor
        qs.push(_key + "=" + _value); // 
    }

    if (!exists) { // se nao encontramos durante o ciclo acima, entao adicionamos uma nova com os valores passados
        qs.push(whatKey + "=" + newValue);
    }

    // aqui retornamos (duh :P)
    return "?" + qs.join("&"); // juntamos o novo array com '&' e ficamos com uma string que podemos usar como querystring
}

Then in your function, you can call this and ask to update the parameter lang with what you received from <select>

function submitForm() {
    var thelang = var thelang = document.getElementById('lang').options[document.getElementById('lang').selectedIndex].value;
    var newquerystring = updateQuerystring("lang", thelang);
    window.location.href = window.location.pathname + newquerystring;
}
    
15.06.2018 / 17:34