Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
Why can't I see the query parameters in the form present in the url?

I'm having a problem getting the form values by passing the two query parameters, id and type, from the url.

Let's assume the URL is:

... page.html?id=14&type=Title

I want the values of these to be shown in the form to then make the change later.

function getQueryVariable () {
             var query = window.location.search.substring (1);
             var vars = query.split ("&");
             for (var i = 0; i <vars.length; i ++) {
                 var pair = vars [i] .split ("=");
             }
         }

         function onLoad () {
             var value = getQueryVariable ();
             var id = document.getElementById ('id');
             var type = document.getElementById ('type');
             id.value = value;
             type.value = value;
         }
<div class = "container">
         <form method ="post" id="save" action="javascript: myFunction()" onload="onLoad()">
             <div class = "field">
                 <label for = "id"> ID: </label>
                 <input type = "number" id = "id" name = "id" />
             </div>
             <div class = "field">
                 <label for = "type"> Fragment Type: </label>
                 <input type = "text" id = "type" name = "type" />
             </div>
             <div class = "field">
                 <button type = "submit" class = "full"> Save changes </button>
             </div>
         </form>
     </div>

As you can see from the code, I call the onLoad() function to load the data into the form.

I don't get any errors; the values of the getQueryVariable() function variables are correct, but I notice that it is not called.

myFunction() is not shown, but this is the function that will serve me later to modify the fields of the form.

Could you kindly help me?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First of all, you need to execute onLoad function, then you need to create an object to store the values and pass it to another function. And your loop is really out of the world, I corrected it, please try to understand my code and if you have questions feel free to comment. Here you go, for query, I changed it to a string that you provided as an exemple, so it would work in snippet.

function getQueryVariable () {
     var object = {}
     var query = "id=14&type=Hello%20Title";
     var vars = query.split ("&");
     for (var i = 0; i <vars.length; i ++) {
         let splitted = vars[i].split('=')
         object[splitted[0]] = decodeURI(splitted[1])
     }
     return object;
 }

 function onLoad () {
     var pairs = getQueryVariable ();
     var id = document.getElementById ('id');
     var type = document.getElementById ('type');
     id.value = pairs.id;
     type.value = pairs.type;
 }
        
  onLoad();
<div class = "container">
         <form method ="post" id="save" action="javascript: myFunction()" onload="onLoad()">
             <div class = "field">
                 <label for = "id"> ID: </label>
                 <input type = "number" id = "id" name = "id" />
             </div>
             <div class = "field">
                 <label for = "type"> Fragment Type: </label>
                 <input type = "text" id = "type" name = "type" />
             </div>
             <div class = "field">
                 <button type = "submit" class = "full"> Save changes </button>
             </div>
         </form>
     </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Your getQueryVariable () function is not returning anything. You can try something like this:

function getQueryVariable () {
             var query = window.location.search.substring (1);
             var vars = query.split ("&");
             var params = {}
             for (var i = 0; i <vars.length; i ++) {
                 var pair = vars [i] .split ("=");
                 params[pair[0]] = pair[1]
             }
             return params
         }

         function onLoad () {
             var params = getQueryVariable ();
             var id = document.getElementById ('id');
             var type = document.getElementById ('type');
             id.value = params.id ;
             type.value = params.type ;
         }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda