Solved

Delete row from mysql database using javascript and php

Posted on 2009-07-03
4
1,199 Views
Last Modified: 2013-12-13
I am new to javascript and php and am trying to build a page with a select list and a delete button.
If the user highlights a value in the select list box which they want to delete and then pushes the delete button, I want the record to be deleted and the current page to be refreshed. Can anyone point me in the right direction?
<?php
//Category Edit Form
 
$username="username";
$password="password";
$database="database";
 
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
 
IF ( ISSET($_POST['orgcodepass']) ) {
$varorgcode = $_POST['orgcodepass'];
$sqlupdate = "delete from tmp_org_service_cat_codes where org_service_cat_code = $varorgcode";
}
 
Print  "<script type='text/javascript'>
function alertSelectedIndex(selId)
{
$.post(Catedit.php,'orgcodepass':'selId'); }
</Script>";
 
//$sql =   "delete from tmp_org_service_cat_codes where org_service_cat_code = $orgcode"
 
Echo "<select name=\"orgmaincatscurrent\" id=\"orgmaincatcodescurrent\" multiple=\"multiple\" size=\"5\">";
 
$querytemp="select b.service_category, b.service_category_code from tmp_org_service_cat_codes a inner join service_cat_lookup b
on a.service_cat_code = b.service_category_code ORDER BY b.service_category";
$resulttemp=mysql_query($querytemp);
 
While ( $rowtemp = mysql_fetch_array($resulttemp) ) {
    echo "<option value = \" " . $rowtemp['service_category_code'] . " \" >" . $rowtemp['service_category'] . "</option>";
}
 
Echo "</select>";
 
print "<input type=\"button\" value=\"alert\" onclick=\"alertSelectedIndex('orgmaincatcodescurrent');\" />";
 
echo "</script>";
 
PRINT "<BR /><BR /><BR />";
 
$query_avail="Select a.service_category, a.service_category_code from service_cat_lookup a
Where a.SERVICE_CATEGORY_CODE not in (select distinct b.SERVICE_CAT_CODE
from tmp_org_service_cat_codes b)";
 
$result_avail=mysql_query($query_avail);
 
Echo "<select name=\"orgmaincatsavail\" id=\"orgmaincatcodesavail\" multiple=\"multiple\" size=\"5\">";
While ( $rowavail = mysql_fetch_array($result_avail) ) {
    echo "<option value = \" " . $rowavail['service_category_code'] . " \" >" . $rowavail['service_category'] . "</option>";
}
Echo "</select>";
print "<input type=\"button\" value=\"alert\" onclick=\"alertSelectedIndex('orgmaincatcodesavail');\" />";
echo "</script>";
 
?>

Open in new window

0
Comment
Question by:geoffcoles
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
Opalelement earned 420 total points
ID: 24773746
Helpful links:

http://www.mredkj.com/tutorials/tutorial006.html adding/removing items
http://www.javascripttoolbox.com/lib/selectbox/examples.php selecting all items(for when you submit the form, you will want to do this onsubmit)
http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html how to capture delete key

Code snippet is to find out the keycode you need in the last link.
<script language=javascript>
<!--
function f()
{
alert(event.keyCode)
}
window.document.onkeydown= f
//-->
</script>

Open in new window

0
 
LVL 7

Assisted Solution

by:szewkam
szewkam earned 80 total points
ID: 24773944
well, but if the page has to be refreshed after deleting the row then using JS isn't neccessary ;). You should just use forms with php :)
0
 
LVL 5

Assisted Solution

by:Opalelement
Opalelement earned 420 total points
ID: 24774013
Didn't notice that lol

Just let them click one and when they hit delete it submits the form, then in PHP delete it:)
0
 
LVL 5

Assisted Solution

by:Opalelement
Opalelement earned 420 total points
ID: 24774015
Thought this was the javascript section, I have been in there all day. Sorry for the JS code:)
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question