1
REPEATED QUESTION TO FACILITATE THE EXAMPLE
I have the following inputs in an HTML (I will show 3, but the actual situation is 20):
<tr>
<td>Digite o código:</td>
<td><input type = "text" id="cod_item" name = "cod_item" size = 10 maxlength =5 placeholder="Código" onChange="getPeca();">
<input type = "text" id="desc_item_1" name = "desc_item_1" size = 30 maxlength =30 placeholder="Descrição">
<input type = "text" id="qtde_item_1" name = "qtde_item_1" size = 5 maxlength =5 placeholder="Qtde"></td>
</tr>
<tr>
<td>Digite o código:</td>
<td><input type = "text" id="cod_item_2" name = "cod_item_2" size = 10 maxlength =5 placeholder="Código">
<input type = "text" id="desc_item_2" name = "desc_item_2" size = 30 maxlength =30 placeholder="Descrição">
<input type = "text" id="qtde_item_2" name = "qtde_item_2" size = 5 maxlength =5 placeholder="Qtde"></td>
</tr>
<tr>
<td>Digite o código:</td>
<td><input type = "text" id="cod_item_3" name = "cod_item_3" size = 10 maxlength =5 placeholder="Código">
<input type = "text" id="desc_item_2" name = "desc_item_2" size = 30 maxlength =30 placeholder="Descrição">
<input type = "text" id="qtde_item_2" name = "qtde_item_2" size = 5 maxlength =5 placeholder="Qtde"></td>
</tr>
The intention is all these cod_item access the query below and display the description in the field desc_item. Example: The user enters the code and Ajax returns the description. Go to field 2 enter the code and the description appears. User can type from 1 up to 20.
$sqlRetorno = 'SELECT descricao from pecas WHERE
cod_peccin = :codItem;';
$resRetorno = $conexao->prepare($sqlRetorno);
$resRetorno->execute(array(
':codItem' => $codItem,
));
$retornos = $resRetorno->fetchAll();
In the initial question I have an Ajax but it works only for one item. In this case, how could all fields do the same query and independently return the description of the entered item, without having to make a script for each field?
Want to send one to one or all in the same ajax? insert one piece by input, or several in the same input? In case there are several inputs you can all be inside the same table?
– Sergio
The inputs are all on the same page and the fields in the table I will save as well. Enter one code at a time, but all 20 are on the same form.
– Diego
vc can pass all variables at once by using serialize, with the syntax: formName.serialize();
– alexandre9865
you cannot do ajax by jquery?
– alexandre9865
Can you give me an example of how to do that?
– Diego
http://api.jquery.com/jquery.post/ this is a way to ajax with jquery, which is easier than with pure javascript
– alexandre9865
https://api.jquery.com/serialize/ serialize documentation...
– alexandre9865
Jesus Diego even you are using snippet stack ("code snippet") no need :/ - The snippet stack is obviously used to run javascript, html and css, that is front-end, there is no reason to use stacksnippet to put php, c++, java, c#, this will never run PHP. If only to display code use normal markup ("code sample" or Ctrl+k).
– Guilherme Nascimento
It was wrong @Guilhermenascimento
– Diego
@Alexandremartinsmontebelo I will try to turn this idea of yours into a solution, if I warn you to answer the question and win the reward.
– Diego
beauty! using this jquery post function, you do ajax in a safe way and in passing the parameters of this function, you only use serialize to then do as if it were a Ubmit on the page, only using ajax ;)
– alexandre9865