Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete an element using Ajax and anchor tag?
    text
    copied!<p>I am trying to create a tagging system like stack overflow. So every tag has a small x beside it so that when a user deletes it, it would be deleted from the database too dynamically. Only users who entered the tags can delete them. I am using an anchor tag for the x and sending the request using Ajax to a php script which would delete the element as follows:</p> <pre><code>&lt;?php require_once("database_connection_handler.php"); $x=$post-&gt;ID; $i=0; $select_query = "Select * from wp_tags inner join wp_posts_tags on wp_posts_tags.tag_id=wp_tags.id where wp_posts_tags.post_id='". $x . "'"; $result = mysql_query($select_query); $num=mysql_num_rows($result); echo "&lt;/ul&gt;"; echoTagHTML(); while ($i &lt; $num) { $tag=mysql_result($result,$i,"tag"); $id=mysql_result($result,$i,"id"); echo "&lt;li&gt;"; tagLinks($tag,$id); if(is_current_user()) { echo "&lt;a href='javascript:void(0)' onclick='deleteTag($id);'&gt;&amp;nbsp x&lt;/a&gt;"; } echo "&lt;/li&gt;"; $i++; }echo "&lt;/ul&gt;&lt;/div&gt;"; ?&gt; </code></pre> <p>The Ajax code that handles this is:</p> <pre><code>function deleteTag(str){ if (str.length != 0){ if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest();} else{// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} xmlhttp.open("GET","links/livesearch.php?del="+str,true); xmlhttp.send(); } else{return;} } </code></pre> <p>The php script is:</p> <pre><code>&lt;?php $del=$_GET["del"]; ConnectToDB(); $delete_query="delete from wp_tags where id='".$del."'"; if(!mysql_query($delete_query)) { echo "error while deleting"; } mysql_close($con); ?&gt; </code></pre> <p>I am really new to Ajax. When I am clicking on the link which is supposed to delete the user it is not deleting anything. It is not removing the tag from the database or removing the link from the page. What am I missing? Any help would be appreciated. Thanks. </p>
 

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