1

How Delete row in table html using ajax and php, I need delete row in html table select row and click button delete make delete using ajax Currentally can make delete without ajax but I need delete row and stay on page without make submit on other page code javaScript

function getDelete()
{


$.ajax({
        type:"post",
        //dataType:"json",
        data:"id="+id,
        url:"delete_address.php?id=$id",   // url of php page where you are writing the query       
 success:function(json)
 { 


 },
error:function(){

        }
        });

}

code html and php

<?php
  $resualt=mssql_query("SELECT * FROM Address where user_id='$UserId' ") ;
  echo "<table border='1' class='imagetable' id='imagetable' 
  width='400px'    >\n";
  echo '<thead>'.'<tr>';
  echo '<th>Street</th>'.'<th>Quarter</th>'.
  '<th>From</th>'.'<th>To</th>'.'<th>Notes</th>';
  echo '</tr>'.'</thead>';
  echo '<tbody>';
  while ($row = mssql_fetch_assoc($resualt)) {
  $fromDate=$row['from_date'];
  $toDate=$row['to_date'];
  echo " <tr onClick='myPopup($row[id])'". 
  ( $_GET['id'] == $row['id'] ?   
  "style='background-color:  green;'":"").">\n"."<td >
  {$row['street']} </td>\n".
  "<td>{$row['quarter']}</td>\n"."<td>$fdate2</td>\n".
  "<td>$tdate2</td>\n"."<td>{$row['other_info']}</td>\n";
 }
 echo '</tbody>';
 echo "</table>\n";
?>
 <?php 
 echo"<a class='button-link' onClick='getDelete()'>delete</a>";
 ?>

code sql query

<?php
 $idEmploye=$_GET['id'];
 $userId=$_GET['user_id'];
 $db_host = 'MOHAMMAD-PC\SQL2005';
 $db_username = 'sa';
 $db_password = '123321';
 $db_name = 'db_test';
 mssql_connect($db_host, $db_username, $db_password);
 mssql_select_db($db_name); 
 mssql_query("DELETE FROM Address
 WHERE id='$idEmploye' ; ") or die(mssql_error()) ;
 echo '<script language="javascript">';
 echo 'alert("successfully deleted ")';
 echo '</script>';
 echo "<script>setTimeout(\"location.href ='address.php';\",10);     </script>"; 

 ?>

Any Help Very Thanks

9
  • how do you define which row to delete? Commented Mar 24, 2015 at 7:44
  • yes. but there is one button only here. so how this button will know which row to delete? Commented Mar 24, 2015 at 7:48
  • first step select row in table after select row show id in url for each row Commented Mar 24, 2015 at 7:51
  • can you rephrase the last comment? didn't understand it. Commented Mar 24, 2015 at 7:51
  • second step click button href direct delete_address.php?id=$id this soultion without ajax but i need make this in ajax Commented Mar 24, 2015 at 7:53

1 Answer 1

1

Try this solution

HTML:

<table>
    <tr>
        <td>Username</td>
        <td>Email</td>
        <td>Action</td>
    </tr>
    <tr>
        <td>TheHalfheart</td>
        <td>[email protected]</td>
        <td>
            <input type="button" class="delete-btn" data-id="1" value="Delete"/>
        </td>
    </tr>
    <tr>
        <td>freetuts.net</td>
        <td>[email protected]</td>
        <td>
            <input type="button" class="delete-btn" data-id="2" value="Delete"/>
        </td>
    </tr>
</table>

We have two button's properties call data-id and class delete-btn

AJAX jQuery:

    <script language="javascript">
    $(document).ready(function(){

        $('.delete-btn').click(function(){

            // Confirm
            if ( ! confirm('Are you sure want to delete this row?')){
                return false;
            }

            // id need to delete
            var id = $(this).attr('data-id');

            // Current button 
            var obj = this;

            // Delete by ajax request
            $.ajax({
                type : "post",
                dataType : "text",
                data : {
                    id : id
                },
                success : function(result){
                    result = $.trim(result);
                    if (result == 'OK'){
                        // Remove HTML row
                        $(obj).parent().parent().remove();
                    }
                    else{
                        alert('request fails');
                    }
                }
            });

        });

    });
</script>

In PHP:

  • Get the ID and delete
  • Reponse OK if success

Sorry i'm learning English, please fix if its bad

Sign up to request clarification or add additional context in comments.

4 Comments

same idea but now i used dynmaic html table and sql
Any help for dynamic table
If possible add friend with me on facebook facebook.com/thehalfheart. Because I can help you any time
Thanks ThehalfHeart I will add

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.