Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to use ajax with php?
    primarykey
    data
    text
    <p>I am trying to show a table on a webpage. Table get content from mysql table. I have a delete button in my table in each row. I want that whenever i click on delete button that row should get deleted from database and webpage both. </p> <p>here is my code</p> <pre><code>&lt;script&gt; function ajaxFunction(str){ if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","ajax-example.php?name="+str,true); xmlhttp.send(); } &lt;/script&gt; &lt;?php // Connect to the database $dbLink = new mysqli('127.0.0.1', 'root', 'root', 'test'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Query for a list of all existing files $sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`'; $result = $dbLink-&gt;query($sql); // Check if it was successfull if($result) { // Make sure there are some files in there if($result-&gt;num_rows == 0) { echo '&lt;p&gt;There are no files in the database&lt;/p&gt;'; } else { // Print the top of a table echo " &lt;table width=100%&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Mime&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Size (bytes)&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Created&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt;"; // Print each file while($row = $result-&gt;fetch_assoc()){ echo " &lt;tr&gt; &lt;td&gt;{$row['name']}&lt;/td&gt; &lt;td&gt;{$row['mime']}&lt;/td&gt; &lt;td&gt;{$row['size']}&lt;/td&gt; &lt;td&gt;{$row['created']}&lt;/td&gt; &lt;td&gt;&lt;a href='get_file.php?id={$row['id']}'&gt;Download&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='checkbox' class='checkbox'&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='button' class= 'button' value='delete' name='delete' onClick='ajaxFunction({$row['name']})'&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='button' class='button' value='edit' name='edit'&gt;&lt;/td&gt; &lt;/tr&gt;"; } // Close table echo " &lt;/table&gt;"; } // Free the result $result-&gt;free(); } else { echo 'Error! SQL query failed:'; echo "&lt;pre&gt;{$dbLink-&gt;error}&lt;/pre&gt;"; } echo "&lt;div id='txtHint'&gt;Yours&lt;/div&gt;"; $dbLink-&gt;close(); ?&gt; </code></pre> <p>here is my ajax-example.php</p> <pre><code> &lt;?php // Connect to the database $name=$_GET['name']; $dbLink = new mysqli('127.0.0.1', 'root', 'root', 'test'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Query for a list of all existing files $sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file` where `name`="$name"'; $result = $dbLink-&gt;query($sql); // Check if it was successfull if($result) { // Make sure there are some files in there if($result-&gt;num_rows == 0) { echo '&lt;p&gt;There are no files in the database&lt;/p&gt;'; } else { // Print the top of a table echo '&lt;table width="100%"&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Mime&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Size (bytes)&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Created&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt;'; // Print each file while($row = $result-&gt;fetch_assoc()) { echo " &lt;tr&gt; &lt;td&gt;{$row['name']}&lt;/td&gt; &lt;td&gt;{$row['mime']}&lt;/td&gt; &lt;td&gt;{$row['size']}&lt;/td&gt; &lt;td&gt;{$row['created']}&lt;/td&gt; &lt;td&gt;&lt;a href='get_file.php?id={$row['id']}'&gt;Download&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='checkbox' class='checkbox'&gt;&lt;input type='button' style='display:none' class= 'button' value='delete' name='delete'&gt;&lt;input type='button' style='display:none' class='button' value='edit' name='edit'&gt;&lt;/td&gt; &lt;/tr&gt;"; } // Close table echo "&lt;/table&gt;"; } // Free the result $result-&gt;free(); } else { echo 'Error! SQL query failed:'; echo "&lt;pre&gt;{$dbLink-&gt;error}&lt;/pre&gt;"; } // Close the mysql connection $dbLink-&gt;close(); ?&gt; </code></pre> <p>This file is actually try to print the row in which i click on the delete button. I am trying to do this first. If the row is getting selected then deleting it won't be a problem. But webpage only show table and pressing delete button won't change content "yours". I don't know what to do. Where i am making mistake?</p> <p>/Note/ This functionality is a part of a feature. Actually those delete button and edit button should be visible when i click on checkbox. For that i use javascript. But here i am not showing it. All i am able to find is that ajaxFunction() is not getting called. Because i try using alert("hi") in it and nothing happen on webpage on clicking on delete button.</p> <p>Any help?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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