php - Data taken from the datatable is always from the first row while running the ajax to delete a row -


hi trying delete data database using j query , ajax. every time delete button clicks,data first row taken. here ajax code

<script type="text/javascript"> $('#mytable').on('click','.b2',function()     {     if (confirm("are sure?"))      {         var id = $('.b2').val();         alert(id);         var datastring = {id : id };         $.ajax({         type: "post",         url: "ajax_delete.php",         data: datastring,         success: function()         {         alert("datas deleted..");         },         error: function()         {             alert("error occured..");            }         });     }     }); 

here php file

<?php   $mysql_host = 'localhost';   $mysql_database = 'proj1';   $mysql_user = 'root';    $mysql_password='';  $mysqli = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);  if (mysqli_connect_errno($mysqli)) { echo 'failed connect mysql: ' . mysqli_connect_error();  }  $id=$_post['id'];  $delete = "delete regestration_details eid=".$id.""; $ss=mysqli_query( $mysqli,$delete);     ?> 

and home page

    <form name="f1" method="post">     <div class="table-responsive">         <table id="mytable" class="display table" style="overflow:auto;" width="80%" >             <thead><tr><th>employee id</th><th>name</th><th>desigation</th><th>blood group</th><th>address</th><th>contact number</th><th></th><th></th></tr></thead>             <?php             $mysql_host = 'localhost';              $mysql_database = 'proj1';              $mysql_user = 'root';              $mysql_password='';             $mysqli = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);                 if (mysqli_connect_errno($mysqli))                 {                     echo 'failed connect mysql: ' . mysqli_connect_error();                 }                 $result=mysqli_query($mysqli,"select *  regestration_details");                     if (mysqli_num_rows($result) > 0)                      {                         // echo "<table cellpadding=10 border=4 background-color:#7e3a18>";                         // echo "<th>" employee id "</th>";                         while($row = mysqli_fetch_row($result))                          {                             echo "<tr>";                             echo "<td>".$row[0]."</td>";                             echo "<td>".$row[1]."</td>";                             echo "<td>".$row[2]."</td>";                             echo "<td>".$row[3]."</td>";                             echo "<td>".$row[4]."</td>";                             echo "<td>".$row[5]."</td>";                             echo"<td><input type='button' name='b1' class='b1' id='bt1' value=".$row[0]." class='btn btn-primary btn-lg btn-block' style='background-color:#55aa00;color:white;height:9%;width:90%;border:0;'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></td>";                             echo"<td><input type='button' name='b2' class='b2' id='bt2' value=".$row[0]." class='btn btn-primary btn-lg btn-block' style='background-color:#d50000;color:white;height:9%;width:90%;border:0;'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></td>";                             echo "</tr>";                         }                         echo "</table>";                     }                     else                      {                          echo "no rows found!";                     }                  ?>             </table>         </div>     </form> 

any please?

the problem in selector:

var id = $('.b2').val(); 

as getting inputs class b2.

to value of clicked input, change to:

$(this).val(); 

and should work


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -