0

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

1
  • i was being stupid, all i needed to do was put return in the onclick bit and add '' to it Commented Mar 23, 2012 at 13:55

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

Actually that's an html table.

You will need to use javascript to delete rows.

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

8 Comments

W3C Boo! Hsss! Only joking your response will probably be slated for it but in many cases it's a suitable reference, unfortunately the cool kids think it's bad so a lot of others say it's bad because they read a paragraph.
but theres php table row data inside that, my javacode will work for table rows outside the php but not inside
I just put that because it's the first thing that came up when I googled "delete a table row in javascript"...and I give the OP the benefit of the doubt that by this point he will know enough to do that if wants better/more info.
@John. No, there is no such thing as a "php table row". You are trying to delete a row in the browser (client side). The browser sees none of your php code (view source on the page in the browser and you will see what I mean). So deleting one of those rows is the same as deleting any other TR element whether hard coded or dynamic.
@SOliver: that's W3S not W3C. I don't know how many people think that the actual W3C is a bad reference. And I think people don't like W3S because they get things wrong and don't fix them, not because they're wrong about everything. w3fools.com
|
0
<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);
?>``

1 Comment

Please add some explanation to improve the answer. Code only is not so good.

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.