HTML input showing PHP result

Asked

Viewed 578 times

-1

I’m starting to play with PHP, and I’m kind of designing a web system. The system would be more or less an idea like this. 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 his name based on a query (for now I am doing with IF, since I do not know database yet, only to play and simulate).

I tried some things here, how can I do?

If you need the code, I can post it too.

  • Put the code always helps.

  • 1

    Put the code in so we can get an idea of how you’re doing

  • you have already managed to do some "things" and such. You were able to post and display on the screen what the user typed. Well, okay, so what exactly is the question? The question, in the second paragraph, is very vague.

  • Hello Rodrigo, from what I understand you have a form, in it you have some fields and want them to be updated based on filling in others, for example, when filling in the ID he inform in the name field the name of the product. If that’s it you have two options, one uses AJAX and javascript, on this you with javascript add a Reader in the form and send a request, receive the answer and via JS you add it in the form, on the other you send the page to the server and return the HTML with the fields already populated (with the value attributes of the inputs filled), it’s more like that?

1 answer

2


For you to do this, it would be ideal to use jQuery/Ajax, because it would become 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: <input type="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 archive php users.:

<?php 
$users = array("1220" => "Cassiano","9090" => "Maria","1522" => "Fulano", "001212" => "Rodrigo");
echo json_encode($users);

A quick explanation why I’m a little busy: in the file "usuarios.php" I created a Array as you can see, each ID of a array (the key) points to user (value), the function json_encode converts this array to format json, after that, the function chamaId() gets the value typed in input, receives the data in jSon of the archive PHP using $.getJSON, and changes the value of the second input with the key typed in the first input, I know the explanation is a little fuzzy, so I’ll edit it and explain it to you better. Take the time to study the code! Good luck.

  • 1

    async: false, with success: function(dados) { does not work, or rather is wrong or better said, there is no need for async:false with Success. Another thing Sjax is considered bad.

  • Okay, I switched to $.getJSON, Forgive my inexperience, I never studied jQuery, but since no one helped Rodrigo, so I answered anyway. But any correction, please feel free to correct the code. Thank you!

  • 1

    Perfect! It was something like this that I wanted! A colleague had told me that I would have to use JS. To the others who commented, thank you equally!

Browser other questions tagged

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