473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to remove table rows from database?

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
6 2674
iam_clint
1,208 Expert 1GB
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
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
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
1,208 Expert 1GB
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
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
Anivia
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

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

Similar topics

2
by: Damien Cobbs | last post by:
Is there a way to remove duplicate rows from a table that does not have a primary key leaving one copy of the row without comparing each column to each other as suggested by the code example below:...
12
by: Rick DeBay | last post by:
I'm trying to create a layout table, where the spacing between rows varies. I've tried using setting margin-top and border-top for the rows I wan't spaced down from the one above, and I've also...
5
by: Harry Gould | last post by:
To all, I'm a newbie here, so please bear with me. I develop web pages for a company intranet where Internet Explorer 6 is the standard. Now I must develop a public internet website that is...
3
by: Bijoy Naick | last post by:
I have something strange going on.. My .aspx page contains a file upload control, a "Import Data" button, a "newTransactions" <asp:table>, a"SaveTrans" button and an confMsg label. First the...
1
by: somaskarthic | last post by:
Hi In my application , i need to create table rows at runtime. Using javascript with table id arguments i could replicate the rows at runtime. Each of these rows has checkboxes (7.no) , dropdown...
7
by: leiño | last post by:
Hi, i am adding table rows dynamically with javascript. This works fine. The table is inside a div with scrolls, initially with 6 rows: ..... <div style='overflow:auto; width:100%;...
1
by: bb nicole | last post by:
Below is my interface for resume which need to post into database... I have did it all in one page and it already can send into database which the table name resume... But my letturer now want me...
2
by: kalyanilovesme | last post by:
Hi I have a html table.. and i am adding the table rows dynamically... actually table rows are 64.. but according to the program some are visible and other are invisible.. i want to calculate...
1
by: premMS143 | last post by:
Hi 1 & all, Using VB, How to remove blank rows in a Excel worksheet? For example, I'm having a Excel sheet containing 900 rows data, in which there are blank rows inserted in between. Manually...
1
gskoli
by: gskoli | last post by:
Dear all Below i have given example of my HTML table structure Where two different <Td> are containing separate table both having same no. of rows but second table having some rows where...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.