Consult BD after leaving the input focus and return results without refreshing?

Asked

Viewed 398 times

0

I have a table that has about 30 input fields to perform a release, example:

<input type="text" size="8" name="Codigo[]" placeholder="Codigo" maxlength="8" />
<input type="text" size="8" name="Codigo[]" placeholder="Codigo" maxlength="8" />
<input type="text" size="8" name="Codigo[]" placeholder="Codigo" maxlength="8" />
<input type="text" size="8" name="Codigo[]" placeholder="Codigo" maxlength="8" />

Here comes the problem, I don’t know anything about jQuery or AJAX, and everything I search through the internet uses examples using them, but they don’t explain what each thing does so I can’t adapt to my need. I work with PHP where this form I get on another page on which I will write the data in the BD, but before I need that when a code is typed in the field and is given the focus in the next field, he searches in the BD the equipment referring to that code and returns the result in a field beside, successively for each of the fields, which are exactly the same as in the example above!

  • 1

    Take a look at this site here: http://jqapi.com/#p=jQuery.ajax ; This is the jQuery documentation. Think about Ajax as an HTTP request made by Javascript, that’s it. The server only needs to respond to the data you ask when sending the request. But I advise you to study jQuery for real: http://fabrica.ms.senac.br/2013/06/jquery-um-simples-tutorial-beginners/

  • Thanks @Daniel, I will rather give a read on these materials you linked here!

1 answer

1


You can do it this way:

When leaving the field sends a request by ajax to a php page, it in turn searches the data as needed and returns to ajax, which adds the data on the screen as desired, follows example:

    //dispara um evento quando sair do campo
    $(".codigo").blur(function(){
    //envia uma requisição por post passando como parâmetro o codigo digitado.
         $.get("enderço da sua página php?codigo="+$(this).val(), 
         function(dados){
          //dados retornados pelo php adicionados na tela onde desejar
          $(dados).appendTo('body');
       })
    })

All you need to do in php is build the html with the data you want to add on the screen:

Receives the data by $_GET["codigo"] -> search in the database -> mount html -> ready

The ajax like this I said will do the same thing as the browser would when we access the page directly, returning all the html generated by php.

  • Thanks @Edson, I still could not do, forgive me for my ignorance, I did not understand how I return the data from the php page to this ajax code that you used example, and then as I get the result and put where I want. in the case where you wrote 'body', it is a variable with the result obtained by php?

  • 1

    All data coming from php will be inside the dados, this "body" is any element on the screen, in this case, the html coming from php through the data can be inserted into an element of the screen. I will try to create a chat room. 1min

  • Okay, I’m standing by!

  • http://chat.stackexchange.com/rooms/info/55710/ajax-jonatasm?tab=general

  • Congratulations @Edson, You helped me a lot! It worked great!

Browser other questions tagged

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