Input HTML showing PHP result

-1

I'm starting to mess around with PHP, and I'm kind of crafting a Web system. The system would be more or less such an idea. The user fills for example a code (ID), say 001212 in an input of type text and below it there is an input of type text that returns the name of it based on a query (for now I am doing with IF, since I do not know database yet, just to play around and simulate).

I've tried a few things here, how can I do it?

If you need the code, I can post too.

    
asked by anonymous 11.06.2015 / 18:14

1 answer

2

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.

    
12.06.2015 / 00:44