John Smith
John Smith

Reputation: 1099

php javascript delete individual row

I have a php table and i want the ability to delete rows of my choice but i am not sure how to go about this. Here is my javascript for deleting:

// When a delete button is pressed in one of the rows 
function deleteCurrentRow(whatrow)
{
  //deletes the approriate row according to what button was pressed
  if (hasLoaded) {
    var delRow = whatrow.parentNode.parentNode;
    deleteRows(delRow);//sends the row for deletion to the function to be deleted
  }
}



function deleteRows(rowforDeletion)
{
  if (hasLoaded) {

      var rowPos = rowforDeletion.sectionRowIndex;//postion of the row for deletion
      rowforDeletion.parentNode.deleteRow(rowPos);//deletes row
      increment = increment -1;

  }
}

Here is my php table:

<table border="1" cellspacing="5" id="tblmarkscheme" style="float: center;">
  <thead>
    <tr>
      <th colspan="5">Template</th>
    </tr>
    <tr>
      <th>Mark</th><th>Criteria</th><th>Delete</th>
    </tr>
  </thead>
  <tbody>
    <?php
      $query = "SELECT maxMark, criteria FROM mark ";
      $result = mysql_query($query);


      while ($row = mysql_fetch_array($result))
      { 
        echo "<tr>";
        echo "<td>" ."<input type = text name = text size = 1 id = mark value = ". $row['maxMark'].">" ."</td>";
        echo "<td>"."<textarea>".$row['criteria']. "</textarea>"."</td>";
        echo "<td>"."<input type = button value = Delete onclick = deleteCurrentRow(this);/>"."</td>";
        echo "</tr>";

      }


    ?>

  </tbody><!--elements added onload and when button pressed -->
</table>

I want to delete individual rows of the table with a press of a button

Upvotes: 0

Views: 2606

Answers (3)

suresh
suresh

Reputation: 1

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function del(id)
{

 var info = 'id=' + id;
    if(confirm("Are you sure you want to delete this Record?")){
        var html = $.ajax({
        type: "GET",
        url: "deletCourse.php",
        data: info,
        async: false ,
         success: function() {
    window.location.reload(true);}
        }).responseText;


    }
}
</script>

<?php
$link=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("cart");
$sql=mysql_query("SELECT * FROM `details`");
echo "<table>";
echo "<tr><th>Name</th><th>NO of Items</th></tr>";
while($row = mysql_fetch_assoc($sql)){
  echo '<tr class="record">';
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['num'] . "</td>";

  echo '<td><div align="center"><a href="#"  id="' . $row['id'] . '" onclick="del('.$row['id'].')">delete</a></div></td>';
  echo '</tr>';
}
echo "</table>";
mysql_close($link);
?>``

Upvotes: 0

Tom
Tom

Reputation: 4180

here's an example; if you click on a button, it's row gets deleted:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">
            var p = {
                deleteRow: function(row) {
                document.getElementById("mytable").deleteRow(row.rowIndex);
                }
            };
        </script>
    </head>
    <body>
        <table id="mytable">
            <tr>
                <td><input type="button" value="row1" onclick="p.deleteRow(this)"/></td>
            </tr>
            <tr>
                <td><input type="button" value="row2" onclick="p.deleteRow(this)"/></td>
            </tr>
        </table>
    </body>
</html>

Upvotes: 1

Mark Fraser
Mark Fraser

Reputation: 3198

Actually that's an html table.

You will need to use javascript to delete rows.

http://www.w3schools.com/jsref/met_table_deleterow.asp

Upvotes: 0

Related Questions