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.
Put the code always helps.
– rray
Put the code in so we can get an idea of how you’re doing
– Otto
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.
– Daniel Omine
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?
– ooredroxoo