Load Value in Input

Asked

Viewed 1,592 times

0

Good Morning,

I am trying to load a value into Input after the user clicks the button.

// Este é o Botão
    <button type="button" class="btn btn-default" tabindex="-1" onclick="id_host()"> Identificar Nome </button>

When he clicks on this button, I want him to go to a page .php and make a gethostname() and return with the result by assigning in the input.

Follow the code for better understanding:

// Função quando Clicar no Botão.
    function id_host() {                    
    var n_pc = $("#conteudo").load("processa.php");
    document.getElementById('#conteudo').value = n_pc;
    }

This is the Input I want popular with the result of processa.php

<input type="text" class="form-control" name="conteudo" id="conteudo" value="">

Can someone help me?

Grateful.

  • The load will load the output from processa.php into the element with id 'content'. Then you should do it var n_pc = $("#conteudo").text();

  • I didn’t understand, in case it would look like this: $("#conteudo").load("processa.php"); and var n_pc = $("#conteudo").text(); ??

  • That’s right. The load does not return anything, it just 'plays' the content in the 'content' element'.

  • Man, it didn’t work out! =/

  • What your processa.php returns, a plain text, an html, a json or what?

2 answers

2

The function load is to load a content (usually some HTML), within a certain element, ie in your case $("#conteudo").load("processa.php"); you are loading the content of processa.php within the element $("#conteudo").

Use the function ajax instead of load, and also cause the result to be placed on callback function. I also recommend performing the return with format json.

Example

$.ajax({
   url: 'processa.php',
   dataType: 'json',
   success: function(data){
      $('#conteudo').val(data.texto);
   },
   error: function(jqXHR, textStatus, errorThrown) {
      console.log(jqXHR);
      console.log(textStatus, errorThrown);
   }
});

parses.php

<?php
   echo json_encode(Array('texto' => 'texto de exemplo'));

Explaining

The part

   success: function(data){
      $('#conteudo').val(data.texto);
   }

will be executed when the ajax receive the return of processa.php, and this return will come in the parameter data. The estate texto of the parameter data, was set there at the processa.php, coding for the format json by function json_encode.

If the script processa.php has some error, and is not executed correctly, so the function success: function(data){ ... } will not be executed, instead the function will be executed error: function(jqXHR, textStatus, errorThrown) { ... }, and the parameters will be objects containing the error information, in it you can put a alert informing the user that the query cannot be completed, thus:

   error: function(jqXHR, textStatus, errorThrown) {
      console.log(jqXHR);
      console.log(textStatus, errorThrown);
      alert('Não foi possível completar a requisição.');
   }

0

Hello, Try to use the method .val()

// Função quando Clicar no Botão.
    function id_host() {                    
    var n_pc = $("#conteudo").load("processa.php");

     //Use o método VAL do jquery
    $("#conteudo").val(n_pc);
    }

If it doesn’t work, try adding one return on the button.

// Este é o Botão
    <button type="button" class="btn btn-default" tabindex="-1" onclick="return id_host();"> Identificar Nome </button>

Browser other questions tagged

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