Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery Ajax remove rows from table and in db
    primarykey
    data
    text
    <p>I have a html table which if filled by values of an mysql table. This way:</p> <pre><code>function getCategories(){ $prod = new C_Product(); $cat= $prod-&gt;getCategorieenAsArray(); $tr = ""; foreach ($cat as $key =&gt; $value){ $tr.="&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;$value&lt;/td&gt;&lt;td&gt;&lt;img src=\"images/delete_button.gif\"&gt;&lt;/td&gt;&lt;/tr&gt;\n"; } return $tr; } </code></pre> <p>Now I want to be able when i press the delete button to remove that row from the database and to refresh the table so that it's actually removed also from the table on the html page. I want to use for this jQuery and Ajax. That shouldn't be to difficult for me. My question is how to select the row that is going to be deleted? How would the jquery know which img is pressed?</p> <p>Should i add a class to the img / tr with a value in it? If I use an id I would have to redefine my jquery function for each row that's being added and thus doesn't seem a right solution.</p> <p>So can anyone please help me further?</p> <p>Thanks</p> <p>//edit:</p> <p>In the mean time I went on my own way to try figure stuff out and I have now a complete ajax call with the correct id. </p> <pre><code>jQuery(document).ready(function(){ jQuery("img.deleterow").click(function(){ id = jQuery(this).parent().attr("id"); jQuery.ajax({ type: "POST", data: "id=" +id, url: "ajax_handler.php", success: function(msg){ jQuery(this).parent().remove(); } }); }); }); </code></pre> <p>Now the only problem I have is to remove the tr from the table when the id is deleted in the database. This is how my tr looks like:</p> <pre><code>$tr.="&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;$value&lt;/td&gt;&lt;td id=\"$key\"&gt;&lt;img class=\"deleterow\" src=\"images/delete_button.gif\"&gt;&lt;/td&gt;&lt;/tr&gt;\n"; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload