joshtheflame Posted August 23, 2009 Share Posted August 23, 2009 Hi All, I am stuck in a problem. I have a program which fills the data in a grid and upon clicking on a button it deletes the record from table. Here are the scripts. this is the actual file where all the codes to fill grid and delete actions. <?php $connection = mysql_connect("localhost", "root", "admin") or die("Error connecting to database"); mysql_select_db("crpl", $connection); $customerid = $_GET["pcusid"]; if ($_GET["recstatus"] == 'D') { $result1 = mysql_query("delete from customers where c_id = '$customerid'", $connection) or die("error querying database"); //echo "1"; } if ($_GET["action"] == 'L') { $result = mysql_query("select c_id, cbname, caddress, cphone, cemail, cpcontact, cpcemail from customers", $connection) or die("error querying database"); echo $header="<table> <caption>Customer Data</caption> <thead> <tr> <th scope='col'>Customer ID</th> <th scope='col'>Name</th> <th scope='col'>Address</th> <th scope='col'>Phone</th> <th scope='col'>Email</th> <th scope='col'>Primary Contact</th> <th scope='col'>Primary Email</th> <th scope='col'>Delete</th> <th scope='col'>Rec</th> </tr> </thead> <tbody>"; $seq=1; while($result_ar = mysql_fetch_assoc($result)) { echo "<tr style='color:#369'> <td>".$result_ar['c_id']."</td> <td>".$result_ar['cbname']."</td> <td>".$result_ar['caddress']."</td> <td>".$result_ar['cphone']."</td> <td>".$result_ar['cemail']."</td> <td>".$result_ar['cpcontact']."</td> <td>".$result_ar['cpcemail']."</td> <td> <a href = javascript:void(0) onclick=delrec('".trim($result_ar['c_id'])."') <img src='images/deleterecord.JPG' border='0' align='bottom' HSPACE='19' VSPACE='2'> </a> <td>".$seq ." </tr>"; $seq++; } echo $footer = "</tbody> </table>"; } ?> here is the JAVASCRIPT file function delrec(p_cusid) { alert(p_cusid); var isdelete = confirm("Confirm customer delete?"); if (isdelete == true) { document.CustomerAdmin.recstatus.value = 'D'; delprocess(thecustomerid); document.CustomerAdmin.recstatus.value = ''; bodyOnLoad(); } else { // do nothing } } function bodyOnLoad() { document.CustomerAdmin.action.value = 'L'; process(); } function getHTTPObject() { if (window.XMLHttpRequest) return new XMLHttpRequest(); else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else { alert("Your browser does not support AJAX."); return null; } } function createQuery() { var elements = document.chartfrm.elements; var pairs = new Array(); for (var i = 0; i < elements.length; i++) { if ((name = elements[i].name) && (value = elements[i].value)) pairs.push(name + "=" + encodeURIComponent(value)); } return pairs.join("&"); } function process() { httpObject = getHTTPObject(); if (httpObject != null) { if (document.CustomerAdmin.action.value == 'L') //Loading of Grid Logic { httpObject.open("GET", "data_files/csadmin_data.php?action="+document.CustomerAdmin.action.value, true); } /* else if (document.chartfrm.flag.value == 'D') { httpObject.open("GET", "data_files/charts_data.php?flag="+document.chartfrm.flag.value+"&accode="+document.chartfrm.accode .value, true); } */ httpObject.send(null); httpObject.onreadystatechange = setOutput; } } function delprocess(x_cusid) { alert(x_cusid); httpObject = getHTTPObject(); if (httpObject != null) { if (document.CustomerAdmin.action.value == 'L') //Loading of Grid Logic { if (document.CustomerAdmin.recstatus.value == 'D') { httpObject.open("GET", "data_files/csadmin_data.php?flag="+document.CustomerAdmin.action.value+"&recstatus="+document.CustomerAdmin.recstatus.value+"&pcusid="+p_cusid, true); } } /* else if (document.chartfrm.flag.value == 'D') { httpObject.open("GET", "data_files/charts_data.php?flag="+document.chartfrm.flag.value+"&accode="+document.chartfrm.accode .value, true); } */ httpObject.send(null); httpObject.onreadystatechange = setOutput; } } function setOutput() { if(httpObject.readyState == 4) { if (document.CustomerAdmin.action.value == 'L') { document.getElementById("customerdiv").innerHTML = httpObject.responseText; //document.CustomerAdmin.testval.value = httpObject.responseText; } /* else if (document.chartfrm.flag.value == 'S') { if (httpObject.responseText == 1) {alert('Transaction Saved Successfully');} reform(); bodyOnLoad();} else if (document.chartfrm.flag.value == 'U') { if (httpObject.responseText == 1) {alert('Transaction Updated Successfully');} reform(); bodyOnLoad();} else if (document.chartfrm.flag.value == 'D') { if (httpObject.responseText == 1) {alert('Transaction Deleted Successfully');} reform(); bodyOnLoad();} */ } } My problem is that its not deleting...I dont know what is the problem..am i passing the parameter right? Also if my data lets say "CustomerID" if its written like this "customer1" then the javascript function "delrec" shows the alert but if the data is like "customer 1" then it doesnt show anything but it shows the error in my mozilla "Unterminated string or literal" ...dont know whats wrong please help. Quote Link to comment Share on other sites More sharing options...
ram4nd Posted August 23, 2009 Share Posted August 23, 2009 this is not a safe way to delete stuff from sql table. Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 You do know that you are not using the variable $result1, which contains the query to delete something, right? Or at least that is what I think. //AngelicS Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 23, 2009 Share Posted August 23, 2009 Hi Couple of things. You are missing the closing tag for the <a> for the onclick (well, it might assume the closing tag of the <img> tag is it). The function delrec doesn't pass on the id of the record to delete to delprocess. It does pass thecustomerid but that isn't initialised. All the best Keith Quote Link to comment Share on other sites More sharing options...
joshtheflame Posted August 23, 2009 Author Share Posted August 23, 2009 I think you forgot to use the HORIZONTAL scrolbar..please scroll towards right side and see there is a tag </a> ... bY THE WAY whats the right way of deleting record from sql table? if you are talking about stripping slashes then that i know and will use later. the major part where i need help is the php script where i used the "delrec" function and passing parameter inside...that i am not doing it correctly...and also in javascript where i declared "Delrec" ...please suggest the code or correct mine. thanks Hi Couple of things. You are missing the closing tag for the <a> for the onclick (well, it might assume the closing tag of the <img> tag is it). The function delrec doesn't pass on the id of the record to delete to delprocess. It does pass thecustomerid but that isn't initialised. All the best Keith Quote Link to comment Share on other sites More sharing options...
ldb358 Posted August 23, 2009 Share Posted August 23, 2009 I think you forgot to use the HORIZONTAL scrolbar..please scroll towards right side and see there is a tag </a> ... bY THE WAY whats the right way of deleting record from sql table? if you are talking about stripping slashes then that i know and will use later. the major part where i need help is the php script where i used the "delrec" function and passing parameter inside...that i am not doing it correctly...and also in javascript where i declared "Delrec" ...please suggest the code or correct mine. thanks Hi Couple of things. You are missing the closing tag for the <a> for the onclick (well, it might assume the closing tag of the <img> tag is it). The function delrec doesn't pass on the id of the record to delete to delprocess. It does pass thecustomerid but that isn't initialised. All the best Keith actually he's right your missing the ">" on the first tag after the on click event so i guess not technically a closing tag but in general the samething Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 23, 2009 Share Posted August 23, 2009 I think you forgot to use the HORIZONTAL scrolbar..please scroll towards right side and see there is a tag </a> ... Thats not what I meant. The > of <a ...... > is missing. Anything that makes deletes easy is risky . Minor changes to the php script:- <?php $connection = mysql_connect("localhost", "root", "admin") or die("Error connecting to database"); mysql_select_db("crpl", $connection); $customerid = ((is_numeric($_GET["pcusid"])) ? intval($_GET["pcusid"]) : 0); if ($_GET["recstatus"] == 'D' and $customerid > 0) { $result1 = mysql_query("delete from customers where c_id = '$customerid'", $connection) or die("error querying database"); //echo "1"; } if ($_GET["action"] == 'L') { $result = mysql_query("select c_id, cbname, caddress, cphone, cemail, cpcontact, cpcemail from customers", $connection) or die("error querying database"); echo $header="<table> <caption>Customer Data</caption> <thead> <tr> <th scope='col'>Customer ID</th> <th scope='col'>Name</th> <th scope='col'>Address</th> <th scope='col'>Phone</th> <th scope='col'>Email</th> <th scope='col'>Primary Contact</th> <th scope='col'>Primary Email</th> <th scope='col'>Delete</th> <th scope='col'>Rec</th> </tr> </thead> <tbody>"; $seq=1; while($result_ar = mysql_fetch_assoc($result)) { echo "<tr style='color:#369'> <td>".$result_ar['c_id']."</td> <td>".$result_ar['cbname']."</td> <td>".$result_ar['caddress']."</td> <td>".$result_ar['cphone']."</td> <td>".$result_ar['cemail']."</td> <td>".$result_ar['cpcontact']."</td> <td>".$result_ar['cpcemail']."</td> <td> <a href='javascript:delrec('".trim($result_ar['c_id'])."') ><img src='images/deleterecord.JPG' border='0' align='bottom' HSPACE='19' VSPACE='2'> </a> <td>".$seq ." </tr>"; $seq++; } echo $footer = "</tbody> </table>"; } ?> Correcting a few errors in the Javascript, mainly variables not being passed through:- function delrec(p_cusid) { alert(p_cusid); var isdelete = confirm("Confirm customer delete?"); if (isdelete == true) { document.CustomerAdmin.recstatus.value = 'D'; delprocess(p_cusid); document.CustomerAdmin.recstatus.value = ''; bodyOnLoad(); } else { // do nothing } } function bodyOnLoad() { document.CustomerAdmin.action.value = 'L'; process(); } function getHTTPObject() { if (window.XMLHttpRequest) return new XMLHttpRequest(); else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else { alert("Your browser does not support AJAX."); return null; } } function createQuery() { var elements = document.chartfrm.elements; var pairs = new Array(); for (var i = 0; i < elements.length; i++) { if ((name = elements[i].name) && (value = elements[i].value)) pairs.push(name + "=" + encodeURIComponent(value)); } return pairs.join("&"); } function process() { httpObject = getHTTPObject(); if (httpObject != null) { if (document.CustomerAdmin.action.value == 'L') //Loading of Grid Logic { httpObject.open("GET", "data_files/csadmin_data.php?action="+document.CustomerAdmin.action.value, true); } /* else if (document.chartfrm.flag.value == 'D') { httpObject.open("GET", "data_files/charts_data.php?flag="+document.chartfrm.flag.value+"&accode="+document.chartfrm.accode .value, true); } */ httpObject.onreadystatechange = setOutput; httpObject.send(null); } } function delprocess(x_cusid) { alert(x_cusid); httpObject = getHTTPObject(); if (httpObject != null) { if (document.CustomerAdmin.action.value == 'L') //Loading of Grid Logic { if (document.CustomerAdmin.recstatus.value == 'D') { httpObject.open("GET", "data_files/csadmin_data.php?action="+document.CustomerAdmin.action.value+"&recstatus="+document.CustomerAdmin.recstatus.value+"&pcusid="+x_cusid, true); } } /* else if (document.chartfrm.flag.value == 'D') { httpObject.open("GET", "data_files/charts_data.php?flag="+document.chartfrm.flag.value+"&accode="+document.chartfrm.accode .value, true); } */ httpObject.onreadystatechange = setOutput; httpObject.send(null); } } function setOutput() { if(httpObject.readyState == 4) { if (document.CustomerAdmin.action.value == 'L') { document.getElementById("customerdiv").innerHTML = httpObject.responseText; //document.CustomerAdmin.testval.value = httpObject.responseText; } /* else if (document.chartfrm.flag.value == 'S') { if (httpObject.responseText == 1) {alert('Transaction Saved Successfully');} reform(); bodyOnLoad();} else if (document.chartfrm.flag.value == 'U') { if (httpObject.responseText == 1) {alert('Transaction Updated Successfully');} reform(); bodyOnLoad();} else if (document.chartfrm.flag.value == 'D') { if (httpObject.responseText == 1) {alert('Transaction Deleted Successfully');} reform(); bodyOnLoad();} */ } } All the best Keith Quote Link to comment Share on other sites More sharing options...
joshtheflame Posted August 24, 2009 Author Share Posted August 24, 2009 Kickstart thank you for pointing my mistake out. Can you please tell me the way I am building this application and the way I have made this grid and deletion procedure via AJAX is the professional way of doing that? or need to polish more skills and change the methods. your suggestion will make me improve my code and skills. thanks Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 24, 2009 Share Posted August 24, 2009 Hi Basics are there and are fine to me. Deleting things easily might not be what you really want. I would be tempted to have a deleted column in the customers table and just mark them as deleted. Further you might want to consider limiting the number of deletes that a single logged on session can perform. I would be inclined to use a CSS floating DIV instead of alerts as you can make them more user friendly. You are using various hidden form fields to store the status of requests and the like. You could just use javascript variables for this. Having a page containing all the customers (non paging) and refreshing them all whenever one is deleted could get a bit impractical if the number of customers gets high. All the best Keith Quote Link to comment 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.