javascript - How to select specific elements using jquery from PHPs while loop generated elements? -


first of all, i'm newbie web programming. said, appreciate if show me in understandable way beginners. i'm trying select , edit while loop generated lists using jquery. want update available members when user clicks edit link , input available member. code follows:

php

       //fetch available staffs     $queryedit =  mysql_query("select * staffs avail = 'yes'");        while($row = mysql_fetch_assoc($queryedit)):        $avail_staffs = $row['name'];        $id = $row['id']; ?>         <h4><?php echo $avail_staffs; ?></h4>          <!--form child of <a> -->         <a href='#' class="edit-<?php echo $id; ?>">edit                  <!--hidden form appears when want edit staffs->            <form id="hidden_form" style="display:none;">                <input type="text" class="staff-name" placeholder="enter staff name">                <button id="edit-<?php echo $id?>">update</button>            </form>        </a>            <?php endwhile ?>  

then tried handle them in jquery this:

jquery

        $(".edit").click(function () {           //display hidden form          $(this).find('form').show();          //when hidden button clicked, grab value in input field & send update.php          $(this).find('button').click(function () {           var staffs = $("input=[class='staff_name']").val();            $.post("internal/update.php",{staffs:staffs},function(data) {              alert("updated");               });              });           }); 

but updates first name. applies change first member name when click last one.

you can this:

$(".edit").click(function () {     //display hidden form     $(this).find('form').show();     //when hidden button clicked, grab value in input field & send update.php     $(this).find('button').click(function () {         var input = []; // create new empty array           $('#hidden_form').each($('input'), function(){ // loop on each of inputs             input.push($(this).val()); // grab input value , add array         });         $.post("internal/update.php",{input:input},function(data) {             alert("updated");         });     }); }); 

haven't tested it, there may few kinks here , there.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -