julianbr Posted December 14, 2012 Share Posted December 14, 2012 Hi, I'm working on a new site. And i can't get my code to work properly. I have already made a site with this script and it works just fine, but it will not work on my new site (wordpress with PHP enable). What I need is an alternative to this code (the delete button command): <td><a onclick="return confirmSubmit()" <a href="?slettID=<?php echo $row_persondata2['id']; ?>"><img src="images/Delete-button.bmp" name="Image3" width="45" height="20" border="0" id="Image3" /></a><a href="?slettID=<?php echo $row_persondata2['id']; ?>"></a> <script LANGUAGE="Javascript"> <!-- // Skript for Confirmasjon-delete function confirmSubmit() { var agree=confirm("Er du sikker på at du vil slette denne hendelsen?"); if (agree) return true ; else return false ; } // --> </script> <a href="update.php?oppdaterID=<?php echo $row_persondata2['id']; ?>"><img src="images/Update-button.bmp" width="45" height="20" border="0" /></a></td> </tr> <?php } while ($row_persondata2 = mysql_fetch_assoc($persondata2)); ?> </table> Take a look at my attachment. What i want is a delete button that is asking "are you sure?" and then if i click yes it delete that row from the database. Is there an easy alternative? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/271986-alternative-code-to-delete-from-sql-database-with-del-button/ Share on other sites More sharing options...
Christian F. Posted December 14, 2012 Share Posted December 14, 2012 Your problem is that your HTML code is invalid, with the first opening anchor tag not being closed, not having a valid href and all that jazz. So delete that first erroneous tag, and move the onchange handler to the actual deletion link. Also, you'll want to move the JS function definition to the header, and not (what appears to be) in the middle of a loop. PS: You should also name the column "Slett/oppdater" and have the button/link read "Slett" as well. Just to keep things consistent. Quote Link to comment https://forums.phpfreaks.com/topic/271986-alternative-code-to-delete-from-sql-database-with-del-button/#findComment-1399331 Share on other sites More sharing options...
julianbr Posted December 14, 2012 Author Share Posted December 14, 2012 Your problem is that your HTML code is invalid, with the first opening anchor tag not being closed, not having a valid href and all that jazz. So delete that first erroneous tag, and move the onchange handler to the actual deletion link. Also, you'll want to move the JS function definition to the header, and not (what appears to be) in the middle of a loop. PS: You should also name the column "Slett/oppdater" and have the button/link read "Slett" as well. Just to keep things consistent. Thank you for fast respond! I'm sorry but i did not understand you here, sorry but i'm just a beginner when it comes to PHP. I try to learn as much as I can The problem with my current code is that when i press delete it does nothing. It just takes me back to the index page without deleting the row. Nor do i get the delete confirmation either. I thought it was easier with an alternative code, but if this would work i would be happy I'll post the whole code so you can see what the whole deal is about. I really appreciated your fast respond and answer ! Thank you Sorry for the mess! Velkommen til det nye admin panelet.! <?php require_once('Connections/pc.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } if ((isset($_GET['slettID'])) && ($_GET['slettID'] != "")) { $deleteSQL = sprintf("DELETE FROM pcbua WHERE id=%s", GetSQLValueString($_GET['slettID'], "int")); mysql_select_db($database_pc, $pc); $Result1 = mysql_query($deleteSQL, $pc) or die(mysql_error()); } mysql_select_db($database_pc, $pc); $query_persondata = "SELECT * FROM pcbua ORDER BY `Tid for innlevering` DESC"; $persondata = mysql_query($query_persondata, $pc) or die(mysql_error()); $row_persondata = mysql_fetch_assoc($persondata); $totalRows_persondata = mysql_num_rows($persondata); mysql_select_db($database_pc, $pc); $query_persondata2 = "SELECT * FROM pcbua WHERE Status <= 'ikke ferdig' ORDER BY `Tid for innlevering` DESC"; $persondata2 = mysql_query($query_persondata2, $pc) or die(mysql_error()); $row_persondata2 = mysql_fetch_assoc($persondata2); $totalRows_persondata2 = mysql_num_rows($persondata2); mysql_select_db($database_pc, $pc); $query_Recordset1 = "SELECT * FROM pcbua WHERE Status= 'utlevert' ORDER BY `Tid for innlevering` DESC"; $Recordset1 = mysql_query($query_Recordset1, $pc) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); mysql_select_db($database_pc, $pc); $query_Recordset2 = "SELECT * FROM pcbua WHERE Status ='utlevert' ORDER BY `Tid for innlevering` DESC"; $Recordset2 = mysql_query($query_Recordset2, $pc) or die(mysql_error()); $row_Recordset2 = mysql_fetch_assoc($Recordset2); $totalRows_Recordset2 = mysql_num_rows($Recordset2); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> <!-- body { background-color: #CCCCCC; background-image: url(Background.png); background-repeat: repeat-x; } --> /*table css */ table.helpT { text-align: center; font-family: Verdana; font-weight: normal; font-size: 11px; color: #404040; width: 980px; background-color: #fafafa; border: 1px #6699CC solid; border-collapse: collapse; border-spacing: 1px; } td.helpHed { border-bottom: 2px solid #6699CC; border-left: 1px solid #6699CC; background-color: #BEC8D1; text-align: left; text-indent: 5px; font-family: Verdana; font-weight: bold; font-size: 11px; color: #404040; } td.helpBod { border-bottom: 1px solid #9CF; border-top: 0px; border-left: 1px solid #9CF; border-right: 0px; text-align: left; text-indent: 10px; font-family: Verdana, sans-serif, Arial; font-weight: normal; font-size: 11px; color: #404040; background-color: #fafafa; } table.sofT { text-align: center; font-family: Verdana; font-weight: normal; font-size: 11px; color: #404040; width: 980px; background-color: #fafafa; border: 1px #6699CC solid; border-collapse: collapse; border-spacing: 0px; } /*hoover*/ table.hovertable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #999999; border-collapse: collapse; } table.hovertable th { background-color:#999; border-width: 1px; padding: 8px; border-style: solid; border-color: #999; } table.hovertable tr { background-color:#f5f4ef; } table.hovertable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #FFF; } </style> <script type="text/javascript"> <!-- function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } //--> </script> </head> <body onload="MM_preloadImages('images/Delete-button-2.bmp')"> Logget på: Adminbruker<br /> <a href="login.php?status=loggedout">Logg ut</a></h6> <h5><span title="Printere"><a href="printadmin.php">Printere</a></span> </a><span title="Registrer Elev"><a href="leggtil.php">Registrer Elev</span> </a><a href="utlevert.php">Utleverte PCer</a><a href="leggtil.php"> </a><span title="Loggen"><a href="elevview.php">Loggen</a><br /> <a href="Status.php">Elev status-side<br /> </a><span title="Info på skjermen!"><a href="info.php">Legg til info<br /> </a><a href="userconfig.php">User Config<br /> </a><a href="innlevering.php">Innlevering</a><a href="userconfig.php"><br /> </a></h5> <h4>Slett eller endre elever her (sortert etter sist oppdatert): <form><input type=button value="Oppdater" onclick="history.go()"></form> </h4> <h6>Sist oppdatert: <?php echo date('l, F j, Y, H:i:s A '); ?> </h6> </h4> <table summary="SuperTabell" class="sofT" cellspacing="0"> <tr> </tr> <tr> <td class="helpHed">Fornavn / Etternavn</td> <td class="helpHed">Status</td> <td class="helpHed">PC-Modell</td> <td class="helpHed">ServiceTAG</td> <td class="helpHed">Klasse</td> <td class="helpHed">Innlevert</td> <td class="helpHed">Oppdatert</td> <td class="helpHed">Service</td> <td class="helpHed">Kommentar</td> <td class="helpHed">LånePC</td> <td class="helpHed">Del or Reload!</td> </tr> </td> </tr> <?php do { ?> <tbody> <tr> <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#fff';"> <td><?php echo $row_persondata2['Elevens navn']; ?></td> <td><?php echo $row_persondata2['Status']; ?></td> <td><?php echo $row_persondata2['Type PC']; ?></td> <td><?php echo $row_persondata2['ServiceTAG']; ?></td> <td><?php echo $row_persondata2['Klasse']; ?></td> <td><?php echo $row_persondata2['time']; ?></td> <td><?php echo $row_persondata2['Tid for innlevering']; ?></td> <td><?php echo $row_persondata2['Service']; ?></td> <td><?php echo $row_persondata2['Kommentar']; ?></td> <td><?php echo $row_persondata2['Lanepc']; ?></td> <td><a onclick="return confirmSubmit()" <a href="?slettID=<?php echo $row_persondata2['id']; ?>"><img src="images/Delete-button.bmp" name="Image3" width="45" height="20" border="0" id="Image3" /></a><a href="?slettID=<?php echo $row_persondata2['id']; ?>"></a> <script LANGUAGE="Javascript"> <!-- // Skript for Confirmasjon-delete function confirmSubmit() { var agree=confirm("Er du sikker på at du vil slette denne hendelsen?"); if (agree) return true ; else return false ; } // --> </script> <a href="update.php?oppdaterID=<?php echo $row_persondata2['id']; ?>"><img src="images/Update-button.bmp" width="45" height="20" border="0" /></a></td> </tr> <?php } while ($row_persondata2 = mysql_fetch_assoc($persondata2)); ?> </table> </tr> </body> </html> <?php mysql_free_result($persondata); mysql_free_result($persondata2); mysql_free_result($Recordset1); mysql_free_result($Recordset2); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271986-alternative-code-to-delete-from-sql-database-with-del-button/#findComment-1399338 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.