How to fill multiple text fields automatically when the main input text receives a value and loses focus?

Asked

Viewed 25 times

0

I have a comic with the tables books and loans. On the table loans, Among other columns, I have one to put the title of the book and the code of this book. The idea is that, by inserting the code of the book and taking the focus of this input, the subsequent field is already filled with the title of the book in question (in this case, both the code and the title come from the table books).

I even get a certain result when I put a static value to test, but even researching materials on how to do this dynamically, I still can’t understand the logic behind this question.

By the way, I used the word "multiple" in the title, because I intend to apply the same reasoning of this question to do the same with other inputs present on the page I am working on.

How to proceed in this situation?

<?php 
$beta = mysqli_query($db, "SELECT titulo FROM livros WHERE codigo_livro='3'"); 

        if (count($beta) == 1 ) {
            $b = mysqli_fetch_array($beta);
            $titulo_livroB = $b['titulo'];
        }

?>

    <input required type="number" name="codigo_livro" placeholder="Insira o código do livro..." value="">
    <input required type="text" name="titulo_livro" placeholder="A ser preenchido automaticamente de acordo com o valor do campo acima." value="<?php echo $titulo_livroB; ?>">

  • 1

    Are you trying to get the user to interact with the page, it updates the data dynamically? If so, give a search on Ajax. PHP acts on the back end, while the user interacts, on front end. In the case of the browser, a Javascript request would be made to the server, which would return the data and the front end would make the dynamic of the thing. On the other hand, using only PHP, you could send the data via POST or GET and the page itself would be updated every time the form was sent, bringing the new information.

  • I managed to make it work, @Weslleyaraújo! I found a material that helped me a lot in this, and, in fact, involves AJAX. Thank you very much for the tip!

No answers

Browser other questions tagged

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