Each or next from a Mysql query

Asked

Viewed 89 times

1

How to post the next item in a multi-item Mysql query?

When to change $( ".membro" ).change(function() { ) send the next data $("membro") to the archive membros.php, via $.post.

Jsfiddle

JS:

$(".membro").change(function () {    
    $.post("membros.php", {
        mes: $(".mes").val(),
        ano: $(".ano").val(),
        celula: $(".celulanome").val(),
        membro: $(".membro").val()
    });    
});

HTML

<tr class=\"tabl\">
    <td id=\"tabela2\" class=\"id\">$row[posicao]
    </td>
    <td id=\"tabela4a\">
    <input type=\"hidden\"  class=\"celulanome\" name=\"membro\" value=\"$celula\">
    <input type=\"hidden\"  class=\"mes\" name=\"membro\" value=\"$mes\">
    <input type=\"hidden\"  class=\"ano\" name=\"membro\" value=\"$ano\">
    <input type=\"text\"  class=\"membro\" name=\"membro\" value=\"$row[membro]\">
    </td>
</tr>


<tr class=\"tabl\">
    <td id=\"tabela2b\" class=\"id2\">$row[posicao]
    </td>
    <td id=\"tabela4b\">
    <input type=\"hidden\"  class=\"celulanome\" name=\"membro\" value=\"$celula\">
    <input type=\"hidden\"  class=\"mes\" name=\"membro\" value=\"$mes\">
    <input type=\"hidden\"  class=\"ano\" name=\"membro\" value=\"$ano\">
    <input type=\"text\"  class=\"membro\" name=\"membro\" value=\"$row[membro]\">
    </td>
</tr>


<tr class=\"tabl\">
    <td id=\"tabela2c\" class=\"id3\">$row[posicao]
    </td>
    <td id=\"tabela4c\">
    <input type=\"hidden\"  class=\"celulanome\" name=\"membro\" value=\"$celula\">
    <input type=\"hidden\"  class=\"mes\" name=\"membro\" value=\"$mes\">
    <input type=\"hidden\"  class=\"ano\" name=\"membro\" value=\"$ano\">
    <input type=\"text\"  class=\"membro\" name=\"membro\" value=\"$row[membro]\">
    </td>
</tr>

PHP

<?php    
include "bd_connect.php";

$membro=$_POST['membro'];    
$celula=$_POST['celula'];
$mes=$_POST['mes'];
$ano=$_POST['ano'];
$posicao=$_POST['posicao'];

if (isset($membro))
{
   $query = mysql_query("UPDATE contas2 SET membro='$membro' WHERE mes LIKE '$mes' AND ano LIKE '$ano' AND celula LIKE '$celula'") or die(mysql_error());
}
?>
  • Can you explain better what you mean by "post the next item"?

  • when the input changes ( $( ". member" ).change(Function() ? ) it sends the data to the members.php file ($. post)

  • Yes, that I can see in c'I say but what is missing?

  • how I sent the value of the next "member" class, when changing the next input

  • So when $( ".membro" ).change(function() { is triggered if I order not this membro: $( ".membro" ).val() but the value of the next member?

  • @Sergio that’s right!!!

Show 1 more comment

2 answers

2

Test like this:

$(".membro").change(function () {
    var proximoMembro = $(this).closest('tr').next().find('.membro');
    $.post("membros.php", {
        mes: $(".mes").val(),
        ano: $(".ano").val(),
        celula: $(".celulanome").val(),
        membro: proximoMembro.val()
    });

});

The idea is:

  • $(this) starting point, the "member" who changed
  • .closest('tr') looking up the DOM tree the first tr
  • .next() go to the next tr
  • .find('.membro'); go down/search within that row the element with the class .membro
  • did not work!!! I used the code exactly as put there!

  • can you explain better what didn’t work? received the wrong data on the php side or not even received data in php? some error on the console?

  • @Alanps saw now that has many fields with the name membro. This will give problems in php, should at least have name="membro[]" to receive an array in php.

  • does not send any value!!

  • How do you know you don’t send? See the console or php doesn’t answer? You can put your php too?

  • put my php!

  • @Alanps puts in first line php var_dump($_POST); and tell me what it gives you. However, before that, you have to correct in HTML for the names (name="") of each input being correct. In PHP you have $mes=$_POST['mes']; but there is no input with name="mes" ...

  • php works perfectly, the error is in js, because if I leave the old Cód it works in the first input!

  • Have you fixed the HTML issues I pointed out above? Have the site online so I can see?

  • Already fixed yes, actually it is part of a restricted area of the site and can not show, but I will try to research the problem better!

  • added a jsfiddle to the post!!

  • Alert result is Undefined and not the value I typed in the input!

Show 7 more comments

0

Managed using each and find!!!

Code:

$(".membro").change(function () {
$('.tabl').each(function() {
var proximoMembro = $(this).find( ".membro" ).val();

    $.post("membros.php", {
        mes: $(".mes").val(),
        ano: $(".ano").val(),
        celula: $(".celulanome").val(),
        membro: proximoMembro
    });

});
});

Browser other questions tagged

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