HeAu
HeAu

Reputation: 11

update data when click a button on each row

I'm a newbie programmer. I need to select then update data when click a button on each row. now I can select and show data but just first update button is working. I think cause of this issue is button id is same. I can't figure out how to create unique id. I try to change id="updatedb" to id="$i" and nothing working

my script is :

    $(function(){
            $('#updatedb').click(function(){
                var a = $('#updatedb').attr('id');                  
                $('#textstatus').val(a);
                alert ($('#updatedb').attr('id'));
            });
    });

and php is :

$sql = $connectdb->query($checknode);
 $i = 0;
    echo "<table border=1>";
    if (!$sql) {
        echo $connectdb->error;
    else {
        while ($row=$sql->fetch_row()) {
            $crow = "updatedb".$i;
            //echo $crow."<br />";
            echo "<form method='post' action=''>";
            echo '<tr><td>'.$row[0].'
            </td><td>'.$row[1].'</td><td>'.$row[2].'
            </td><td>'.$row[3].'</td><td>'.$row[4].'
            </td><td>'.$row[5].'</td><td>
            <input  type="button" id="updatedb" name="updatedb" value="update">
            <input type="text" id="textstatus"  name="textstatus" 
            value="" disabled="disabled"></td></tr>';
            $i++;
        }
    }
echo "</form>";
echo "</table>";

Upvotes: 1

Views: 1024

Answers (1)

Varun Malhotra
Varun Malhotra

Reputation: 1202

You can use class rather then id for button to update see the code below

jquery

$(function(){
            $('.updatedb').click(function(){
                var a = $(this).attr('class');                  
                $(this).parents('tr').find('.textstatus').val(a);
                alert (a);
            });
    });

php

$sql = $connectdb->query($checknode);
 $i = 0;
    echo "<table border=1>";
    if (!$sql) {
        echo $connectdb->error;
    }else
        {
        while ($row=$sql->fetch_row()) {
            $crow = "updatedb".$i;
            //echo $crow."<br />";
            echo "<form method='post' action=''>";
            echo '<tr><td>'.$row[0].'
            </td><td>'.$row[1].'</td><td>'.$row[2].'
            </td><td>'.$row[3].'</td><td>'.$row[4].'
            </td><td>'.$row[5].'</td><td>
            <input  type="button" class="updatedb" name="updatedb" value="update">
            <input type="text" class="textstatus"  name="textstatus" 
            value="" disabled="disabled"></td></tr>';
            $i++;
        }
    }
echo "</form>";
echo "</table>";

Upvotes: 1

Related Questions