How to send names from a php loop to another input using JS

Asked

Viewed 113 times

-2

How to pass data to another input other than id? example value name not id.

function passar(id){ 
    var valorA = document.getElementById(id); 
    var nome = document.getElementById("nome"); 
    nome.value = valorA.value; 
}; 
<?php
         $sql_lista = mysql_query("SELECT*FROM login LIMIT 0, 2"); 
		  //aqui fazemos a contagem para exibição se caso ouver dados(quantidade)
		 $sql_contar = mysql_num_rows($sql_lista);
		 		 ?>

  <?php
     while($resultado = mysql_fetch_array($sql_lista)){ ?>    

<?php echo $resultado['usuario'];?>
 
    <form>

<input 
    TYPE="text" 
    SIZE="3" 
    name="valorA" 
    id="valorA_<?=$resultado['lo_id'] ?>" 
    value="<?php echo $resultado['usuario'];?>" 
/>
 
<button type="button" onclick="passar(<?=$resultado['usuario']?>);"> passar valores </button>

   
 </form>
        
  <?php
  //fechamos o while
		 } 
  ?>
 Nome:<input type="text" id="nome" size="10"/> 

  • what value you want to pass?

  • type the following I want to pass id and the user name the two separate have as?

1 answer

1


I’ll tell you what:

//Aqui você precisa passar o ID do seu campo
function passar(id){ 

    //Note que o ID do seu campo é composto por 'valorA_' + ID do usuario
    var id_campo = 'valorA_'+ id
    var valorA = document.getElementById(id_campo);

    var nome = document.getElementById("nome"); 

    nome.value = valorA.value; 
}; 

There is a second option which is to pass tbm the name by function parameter:

<button type="button" onclick="passar(<?=$resultado['lo_id']?>,'<?= $resultado['usuario'] ?>');"> passar valores </button>

But note that in your role passar(), you already take the value of the value field_.

Follow example of working code:

<script>
function passar(id){ 
    var valorA = document.getElementById('valorA_'+id); 
    var nome = document.getElementById("nome"); 
    nome.value = valorA.value; 
};
</script>
<?php
//Esse array é apenas para substitui sua consulta no banco nesse exemplo.
$resultado = array();
$resultado['lo_id'] = 1;
$resultado['usuario'] = 'John Doe';

?>
<form>
    <input TYPE="text" SIZE="15" name="valorA" id="valorA_<?=$resultado['lo_id'] ?>" value="<?php echo $resultado['usuario'];?>" />

    <button type="button" onclick="passar(<?=$resultado['lo_id']?>);"> passar valores </button>

</form>

 Nome:<input type="text" id="nome" size="10"/> 

I hope I’ve helped!

  • Then my bhroter Valew by the accurate tip of the idea I realized that the id was with a certain plate, this was the idea of how to make face helped me a lot I’m advancing my tcc, and taking advantage to learn more. Thank you

  • Do you know why I can’t select within the input value by calling a variable which is the line id? <input TYPE="text" SIZE="3" name="value" id="valueA_<?= $result['via_id'] ? >" value="<? php $id_t= $resultado['via_id'];&#xA;$sql_listaB = mysql_query("select format(sum(ar_valor ), 2) as valort from adiantamento_rel where adiantamento_depo_id_ad='$id_t'; "); $resulB = mysql_fetch_array($sql_listaB); $valort = $resulB ['valort']; echo $valort; ? >" />

  • @Ezer boy, make this select out of the input, your code will become more understandable. Take a look at my answer on your other question, see if it answers you.

  • @Ezer see the edited answer, put a code snippet working the pass name.

  • made the tested changes several times and once again helped me kkk changed to what I wanted it is more complex rs and yes really the select in the bank had to be out even made a Join Inner from another table to calculate a certain value.

  • Oops... I’m glad I could help again!

Show 1 more comment

Browser other questions tagged

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