By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,259 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,259 IT Pros & Developers. It's quick & easy.

How to remove table rows from database?

P: 4
I need to be able to delete table rows from database via link which is shown down below, but i don't know how to form javascript function which will enable that.

So, this is how my index.php looks like:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <link rel="stylesheet" type="text/css" media="screen" />
  5. <title>Ajax</title>
  6.  
  7.  
  8.  
  9. <script>
  10.  
  11.  
  12.  
  13. function dodajNovogAutora()
  14. {
  15. alert("Dodali ste novog autora!");
  16.      var ime = document.getElementById("inputName").value;
  17.       var prezime = document.getElementById("inputPrezime").value;
  18.       var adresa = document.getElementById("inputAdresa").value;
  19.      alert("add.php?ime=" + ime + "&prezime=" + prezime + "&adresa=" + adresa);
  20. var xmlhttp;
  21. if (window.XMLHttpRequest)
  22.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  23.   xmlhttp=new XMLHttpRequest();
  24.   }
  25. else
  26.   {// code for IE6, IE5
  27.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  28.   }
  29. xmlhttp.onreadystatechange=function()
  30.   {
  31.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  32.     {
  33.  
  34.  
  35.       var rowCount = document.getElementById("tablica").getElementsByTagName("TR").length;
  36.  
  37.       var table=document.getElementById("tablica");
  38.       var row=table.insertRow(rowCount);
  39.       var cell1=row.insertCell(0);
  40.       var cell2=row.insertCell(1);
  41.       var cell3=row.insertCell(2);
  42.       var cell4=row.insertCell(3);
  43.       var cell5=row.insertCell(4);
  44.       var cell6=row.insertCell(5);
  45.       var sifra= xmlhttp.responseText;
  46.       cell1.innerHTML=sifra;
  47.       cell2.innerHTML=ime;
  48.       cell3.innerHTML=prezime;
  49.       cell4.innerHTML=adresa;
  50.       cell5.innerHTML="<a href=\"#\" onclick=\"updateauthor()(" +sifra + ");return false;\">Edit</a>";
  51.       cell6.innerHTML="<a href=\"#\" onclick=\"deleteauthor()(" +sifra + ");return false;\">Briši</a>";
  52.  
  53.     }
  54.   }
  55.  
  56.  
  57. xmlhttp.open("GET","add.php?ime=" + ime + "&prezime=" + prezime + "&adresa=" + adresa,true);
  58. xmlhttp.send();
  59. }
  60. //potrebno je napraviti funkcije update i obrisi koje rade kao i dodajNovogAutora
  61.     </script>
  62.  
  63.  
  64.  
  65.  
  66.  
  67. </head>
  68.  
  69. <body>
  70. <h3>Popis autora</h3>
  71. <table border="1" id="tablica">
  72.  
  73. <thead>
  74.     <tr>
  75.         <td>Sifra</td>
  76.         <td>Ime</td>
  77.         <td>Prezime</td>
  78.         <td>Adresa</td>
  79.         <td></td>
  80.         <td></td>
  81.     </tr>
  82. </thead>
  83. <tbody>
  84.  
  85. <?php    
  86.     include 'connect-db.php';
  87.     //dohvacanje rezultata pretrazivanja
  88.     $rezultati = mysql_query("select * from Autor");
  89.     //Iteracija kroz rezultate 
  90.     while($red = mysql_fetch_array($rezultati))
  91.     {
  92.         //ispis rezultata pretrazivanja
  93.         echo "<tr>";
  94.         echo " <td>" . $red['sifra'] . "</td>";
  95.         echo " <td>" . $red['ime'] . "</td>";
  96.         echo " <td>" . $red['prezime'] . "</td>";
  97.         echo " <td>" . $red['adresa'] . "</td>";
  98.  
  99.  
  100.         echo " <td><a href=\"#\" onclick=\"updateauthor()(". $red['sifra'] . ");return false;\">Edit</a></td>";
  101.         echo " <td><a href=\"#\" onclick=\"deleteauthor()(". $red['sifra'] . ");return false;\">Delete</a></td>";
  102.  
  103.         echo "</tr>";
  104.     }
  105.     mysql_close($veza);
  106. ?>
  107.  
  108.  
  109.  
  110.  
  111.  
  112. </tbody>
  113. </table>
  114.  
  115.  
  116. <h3>Dodaj novog autora</h3>
  117. <form action="add.php" method="GET" id="forma">
  118.     Ime: <input type="text"  name="inputName" id="inputName" value="" /> <br />
  119.     Prezime: <input type="text" name="inputPrezime" id="inputPrezime" value="" /><br />
  120.     Adresa: <input type="text" name="inputAdresa" id="inputAdresa" value="" />
  121.  
  122.     <br />
  123.     <input type="button" onclick="dodajNovogAutora(); return false;" value="Dodaj" />
  124. </form>
  125.  
  126. </body>
  127. </html>
  128.  
  129.  
  130. And this is how my delete.php looks like:
  131.  
  132. <?php
  133.     //Uspostava veze prema bazi
  134.     $veza = mysql_connect("localhost","root","");
  135.     //odabir baze na kojoj zelimo raditi
  136.     mysql_select_db("seminar", $veza);
  137.  
  138.     mysql_query("DELETE FROM Autor WHERE sifra = $_GET[sifra]") or die(mysql_error());
  139.  
  140.  
  141. ?>
So could anyone help me, and form a javascript function which will enable deleting rows by clicking 'Delete' link?
Know it's a few lines of code, but I just can't do it.
Sep 24 '12 #1
Share this Question
Share on Google+
6 Replies


iam_clint
Expert 100+
P: 1,208
well you would do the same thing you have done using "ajax" but sending a different query string and parsing it with your php to delete rows in the database?
Sep 24 '12 #2

P: 4
Don't understand what you're trying to say cause its a bit new to me.
All i need is to make 2 more functions(for delete table row in database, and edit), cause i've got add new function already (don't know why i pasted it cause it's actually not needed).
Sep 24 '12 #3

P: 4
Delete function should refer to this row

Expand|Select|Wrap|Line Numbers
  1. echo " <td><a href=\"#\" onclick=\"deleteauthor()(". $red['sifra'] . ");return false;\">Delete</a></td>";
Sep 24 '12 #4

iam_clint
Expert 100+
P: 1,208
I can't give you the exact code i don't have enough information but you need to make another ajax request to your server in the function

From previous post
Expand|Select|Wrap|Line Numbers
  1.  xmlhttp.open("GET","add.php?ime=" + ime + "&prezime=" + prezime + "&adresa=" + adresa,true);
  2. xmlhttp.send();
  3.  
in add.php or even make a dele.php if you feel the need to and make it remove the records based on the criteria passed.
Sep 24 '12 #5

P: 4
Since i'm actually new to this, don't wonder if i'm asking 'well-known or stupid questions', but does that mean that i should make a new function and copy these two lines that u quoted and add delete.php instead of add.php in new function?

I've already got delete.php that contains criteria which should allow deleting records from database, just don't know how to form that function and write the code in index.php :[
Sep 25 '12 #6

P: 1
Well hello mate... I think you should be able to do this with your knowledge, as you are very intelligent and all that. It is of utmost importance that you do that. Thanks!
Sep 25 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.