Insert mysql with Ajax inside While

Asked

Viewed 151 times

0

I’m trying to make a dynamical Insert using ajax. as shown in the image below. I want to be able to click send from any line and do the post referring to it

inserir a descrição da imagem aqui

How can I do that

<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<table style="width: 20%;">
<thead>
    <tr>
        <th style="width: 1%;" class="center">Cod.</th>
        <th style="width: 18%;" >Produtos</th>
        <th class="text-center" style="width: 8%;">Valor</th>
        <th style="width: 5%;"></th>
    </tr>
</thead>
<tbody>                 

  <?
    $cmd = "SELECT * FROM produtos where id_transfer = '1' limit 5";      
    $produtos = mysql_query($cmd)or die( mysql_error());
    $count=mysql_num_rows($produtos);
    while ($linha = mysql_fetch_array($produtos)) {


 $id_produto = $linha['id_produtos'];
 $id_transfer = $linha['id_transfer'];
 $cheva = $linha['chave'];
?>                  


        <tr class="selectable">
        <td class="center"><input type="text" id="campo1" value="<?echo $id_transfer1?>" /></td>
        <td class="important"><input type="text"   id="campo2" value="<?echo "$id_produto"?>" /></td>
        <td class="text-center"><input type="text" id="campo3" value="<?echo $chave?>" /></td>
        <td class="center">
        <input type="submit" class="btn btn-danger" onclick="inserir_registo()" /><i class="fa fa-arrow-right"></i></i></a>
        </td>
        </tr>
  • See if it has anything to do with having a $cheva = $linha['chave']; and a <?echo $chave?>. Typo?

  • That variable doesn’t even make a difference. independent of any variable you have only sends the while pepper line let’s say the first line has id 1 the second 2 ...... only sends id 1

  • But why didn’t you just put an upload button? and the fields in array format? Because then when it’s time to send, send everything at once and record the filled...

  • Because in fact all these fields will stay hidden, only the buttons will appear. because this , this system will stay on a touch screen and I wanted to just click on the item is already included for the base

1 answer

0

The problem is in replicating field ID, this cannot occur.

See that inside your while vc replicates the property ID of your input. Instead of doing this, put one ID per line like this:

<% while ($linha = mysql_fetch_array($produtos)) { %>

<tr data-id="<%echo idDaLinha%>">
    <td><input type="text" name="campo1" /></td>
    <td><input type="text" name="campo2" /></td>
    <td><input type="text" name="campo3" /></td>
    <td><input type="button" class="botaoEnviar" data-id="<%echo idDaLinha%>">Enviar</button>
</tr>

<% } %>

And in your jQuery:

$('.botaoEnviar').click(function() {
    var id = $(this).attr('data-id');
    var $tr = $('tr[data-id='+ id +']');

    var dadosAjax = 
    {
        campo1: $tr.find('input[name=campo1]').val(),
        campo2: $tr.find('input[name=campo2]').val(),
        campo3: $tr.find('input[name=campo3]').val()
    };

    // sua chamada ajax
});
  • Ola Tiago do not have much knowledge of javascript and do not know why I could not adapt your code in my javascript. did not call ajax

  • I made the code https://jsfiddle.net/fabioo7/wcu6qkn8/ what I did wrong?

  • <input type="button" class="botaoEnviar" data-id="<?echo idDaLinha?>">Enviar&#xA;</button> That dude ? On line 16 you opened an input and closed with button

Browser other questions tagged

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