Return Ajax value in input

Asked

Viewed 378 times

1

I built a query using php+oracle to fetch the employee name from the badge. I searched and found a script that queries and returns on the same page using Ajax. But it returns the value only in the entire div. How do I return in a field I define? Example:

 function getDados() {

 // Declaração de Variáveis
 var nome   = document.getElementById("txtnome").value;
 var result = document.getElementById("Resultado");
 var xmlreq = CriaRequest();

 // Exibi a imagem de progresso
 result.innerHTML = '<img src="images/Progresso.gif"/>';

 // Iniciar uma requisição
 xmlreq.open("GET", "processa.php?txtnome=" + nome, true);

 // Atribui uma função para ser executada sempre que houver uma mudança de ado
 xmlreq.onreadystatechange = function(){

     // Verifica se foi concluído com sucesso e a conexão fechada (readyState=4)
     if (xmlreq.readyState == 4) {

         // Verifica se o arquivo foi encontrado com sucesso
         if (xmlreq.status == 200) {
             result.innerHTML = xmlreq.responseText;
         }else{
             result.innerHTML = "Erro: " + xmlreq.statusText;
         }
     }
 };
 xmlreq.send(null);
 }

In this case the variable "Result" applies the result within the div Result, but not to the inputs that you set to be fed....

1 answer

1


Substitute:

result.innerHTML = xmlreq.responseText;

For:

document.getElementById("iddomeucampo").value = xmlreq.responseText;

getting:

function getDados() {

 // Declaração de Variáveis
 var nome   = document.getElementById("txtnome").value;
 var result = document.getElementById("Resultado");
 var xmlreq = CriaRequest();

 // Exibi a imagem de progresso
 result.innerHTML = '<img src="images/Progresso.gif"/>';

 // Iniciar uma requisição
 xmlreq.open("GET", "processa.php?txtnome=" + nome, true);

 // Atribui uma função para ser executada sempre que houver uma mudança de ado
 xmlreq.onreadystatechange = function(){

     // Verifica se foi concluído com sucesso e a conexão fechada (readyState=4)
     if (xmlreq.readyState == 4) {

         // Verifica se o arquivo foi encontrado com sucesso
         if (xmlreq.status == 200) {
             document.getElementById("iddomeucampo").value = xmlreq.responseText;
         }else{
             result.innerHTML = "Erro: " + xmlreq.statusText;
         }
     }
 };
 xmlreq.send(null);
 }
  • I did as you said, friend, but got into line: result.innerHTML = '<img src="images/Progress.gif"/>'; and did not get out anymore. I checked the name of the div and the input and they are correct. Any ideas? Thanks for the help

  • Document.getElementById("Result") exists?

  • I changed the div to "Content" and the input is "Func". The names in both ajax.js and php are the same....

  • Open the browser console and see the error

  • I created another div, with another name and same field name. Then he left the progress running in the new div and then yes put the value in the div that wanted. I will tell the progress bar and reversed all values, pointed to one div and it stored in the other. How do I see the errors in the console? Thank you

  • It depends on the browser that Voce ta using. If you post the whole code facilitates

  • Friend, thanks for your help. I removed the perfume from that progress bar and exactly with your code it worked. It was without the "researching", but at the moment there is no problem. Again, thanks for the help!

  • Quiet. Hugs

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.