Grab specific button with Jquery in repeat structure

Asked

Viewed 367 times

0

while($reg = $stmt->fetch(PDO::FETCH_OBJ))
      {
         $id = $reg->id;

echo "<div class='altpost' id='$id'></div>";
        echo "<form id='$id' class='faltpost' method='post' action='altp.php'><input name='naltpost' type='number' value='$id' hidden/><button>editar</button></form>";
      }

jQuery(document).ready(function(){
        jQuery('.faltpost').submit( function(){
            var dados = jQuery(this).serialize();
            jQuery.ajax({
                url: "altp.php",
                type: "POST",
                data: dados,

                success: function(data)
                {
                    var id = $('.altpost').attr("id");
                    $(document.getElementById(id)).html(data);

                }

            });

            return false;
        });
    });

When I click on any button of any post only activates the effect in the first post, the problem is that the buttons have the same name, looking for a way to specify each button. I tried jQuery('.faltpost'). attr("id"). Ubmit, but it didn’t work. the "$id" is the number of the post, I’m taking a beating from JQUERY who help me is sure I will give score.

  • 1

    The div and the form have the same value in id? This is not possible. The attribute id defines a single element on the page and no more than one element with the same id. Read more on W3C specification.

  • Because I had put class and changed it to id, I forgot that, I will change to class again, but the problem here is how to specify the button in Jquery. I click and select all buttons, because I don’t know how I do to grab a specific button and put in Jquery with while, because it repeats all posts with class with the same name. When I try to play for Jquery it takes them all at once, because I cannot specify in Jquery:

  • The submit form is working correctly? Only the result display that appears on all elements?

  • The problem is in this line here: "jQuery('. faltpost'). Submit( Function(){" The class faltpost is in all buttons. I want to specify each of them.

  • It’s working normally, but only in the first post, if I press the bottom post, the effect works in the first post and in any other post the same thing.

  • Yes, but you need to do this, but the function will be executed only for the form that is submitted. The real problem is in the function success. You’re always getting the value of id of $('.altpost'), that will always be the first.

  • The first time I tried I was doing it this way

  • Success: Function(data){ $(".altpost"). html(data); }

  • But so the effect goes to all posts

  • Do you have any suggestions so I can solve this problem?

Show 5 more comments

1 answer

1


What I would recommend you do is put both the div for the form within the same parent element. For example:

<div class="result">
    <div class='altpost'></div>
    <form class='faltpost' method='post' action='altp.php'>
        <input name='naltpost' type='number' value='$id' hidden />
        <button>editar</button>
    </form>
</div>

In this way, we can make Javascript code as follows:

// Tratamento do evento submit do formulário:
$(".faltpost").submit(function () {

    // Objeto do formulário que foi submetido:
    var form = $(this);

    // Recupera os dados a serem enviados:
    var dados = form.serialize();

    // Envia os dados através de AJAX:
    $.ajax({
        url: "alt.php",
        method: "post",
        data: dados,
        success: function (data) {

            // Objeto da div referente ao formulário:
            var div = form.parent(".result").children(".altpost");

            // Exibe o conteúdo na div:
            div.html(data);

        }
    }); 

});

The question is to do:

var div = form.parent(".result").children(".altpost");

This will look for the parent element of the form that has the class .result, then search this element for a child element that has the class .altpost. In this way, the div which is attached to the div.result.

  • I’ll try here! I’ll tell you soon if it worked. : )

  • It worked here, thank you very much! I’ve been trying for a long time and I couldn’t do it.

  • I would give like, but here’s saying it doesn’t because I don’t have 15 of reputation. When I have I give like!

Browser other questions tagged

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