For you to do this, it would be ideal to use jQuery/Ajax
because it would be more dynamic.
It would look something like this in HTML:
<html>
<title>Meu site</title>
<head>
<script type="text/javascript" language="JavaScript" src="https://code.jquery.com/jquery-latest.js"></script></head><body>Id:<inputtype="text" name="id" /><br/>
Nome: <input type="text" name="nome" /><br/><p>
<button onclick="chamaId();">Conferir</button>
</body>
<script>
function chamaId(){
$.getJSON('usuarios.php', function(dataf) {
id = $('input[name=id]').val();
nome = dataf;
if(!nome[id] == ""){
$('input[name=nome]').val(nome[id]);
} else {
$('input[name=nome]').val("ID "+id+" inexistente");
}
});
}
</script>
</html>
And in the users.php file:
<?php
$users = array("1220" => "Cassiano","9090" => "Maria","1522" => "Fulano", "001212" => "Rodrigo");
echo json_encode($users);
A quick explanation why I'm kind of busy:
in the file "users.php" I created a Array
as you can see, each ID of a array
( key ) points to user ( value ), function json_encode
converts this array to json
format, after that, the chamaId()
function gets the value entered in the input
, receives the data in jSon
from the PHP
file using $.getJSON
, and changes the value of the second input
with the key typed in the first input
,
I know the explanation is half confused, then I edit and explain it better for you.
Take the time to study the code!
Good luck.