ameriblog Posted July 31, 2007 Share Posted July 31, 2007 I have a pretty simple PHP script that deletes an entry from the database. I'd like to add AJAX so that it doesn't change pages on delete, instead does it via AJAX and keeps me on that page. I have: <strong>Story Title</strong> <br /><a href="delete.php?newsID=1">Delete</a> Here is my current delete.php: <? // includes include("xxxx.php"); // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // generate and execute query $query = "DELETE FROM news WHERE newsID='" . $_GET['newsID'] . "'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close database connection mysql_close($connection); header ( "http://www.mysite.com/list.php" ); ?> Thanks for any help... Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/ Share on other sites More sharing options...
Philip Posted July 31, 2007 Share Posted July 31, 2007 Have you started with any AJAX yet? Why not look at this one: question that is almost the same, just solved That should get you started, read through it, try it out, and come back with questions Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-312055 Share on other sites More sharing options...
ameriblog Posted July 31, 2007 Author Share Posted July 31, 2007 Here is what I have, it isn't working. For some reason it isn't deleting, or working at all. Also, I'd like to have it delete via a text link, not an input button. <? require ( "db_connect_file.php" ); $posts_rs = $conn->Execute ( "SELECT ID, post_title, post_modified FROM wp_posts ORDER BY post_modified DESC" ) or die ( $conn->ErrorMsg() ); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> unction createXMLHttp() { if (typeof XMLHttpRequest != 'undefined') return new XMLHttpRequest(); else if (window.ActiveXObject) { var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.5.0"]; for (var i = avers.length -1; i >= 0; i--) { try { httpObj = new ActiveXObject(avers[i]); return httpObj; } catch(e) {} } } throw new Error('XMLHttp (AJAX) not supported'); } var request = createXMLHttp(); function sendRequest(request, url) { request.open("GET", url + '&GUINum=' + Math.floor(Math.random()*99999999999999999), true); // The math random is for IE to not cache it request.onreadystatechange = finishUpdate; request.send(null); } function confirmit() { input_box=confirm("Are you sure you want to delete this item?"); if (input_box==true) { var ID = gup(ID); var url = "delete.php?ID=" + ID; sendRequest(request, url) } else { //Abort part (I changed the text on page displaying 'aborted!' } } function finishUpdate() { if (request.readyState == 4) { if (request.status == 200) { //do whatever when PHP script is done } } } </script> </head> <body> <? while ( ! $posts_rs->EOF ) { ?> <?=($posts_rs->Fields("post_title"))?> <input type="button" value="Delete" id="sendButton" onClick="confirmit();" /> <br /><br /> <? $posts_rs->MoveNext(); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-312102 Share on other sites More sharing options...
Philip Posted August 1, 2007 Share Posted August 1, 2007 Okay, then here: <input type="button" value="Delete" id="sendButton" onClick="confirmit();" /> Change it to <a href="delete.php" onClick="confirmit(); return false">linkie</a> Are you getting the variable from the URL to view the page where you have the option to delete? (viewitem.php?ID=1 -> it must be ID because of [var ID = gup(ID);]) Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-312579 Share on other sites More sharing options...
ameriblog Posted August 2, 2007 Author Share Posted August 2, 2007 I set the link to be: <a href="delete.php?ID=<?=($posts_rs->Fields("ID"))?>" onClick="confirmit(); return false"><?=($posts_rs->Fields("post_title"))?></a> It does delete the file, but it doesn't do it via AJAX, it takes me to delete.php Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-314303 Share on other sites More sharing options...
ameriblog Posted August 13, 2007 Author Share Posted August 13, 2007 Any help/ideas? Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-322444 Share on other sites More sharing options...
Philip Posted August 17, 2007 Share Posted August 17, 2007 Are you getting the variable from the URL to view the page where you have the option to delete? (viewitem.php?ID=1 -> it must be ID because of [var ID = gup(ID);]) Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-326870 Share on other sites More sharing options...
mainewoods Posted August 17, 2007 Share Posted August 17, 2007 <a href="delete.php?ID=<?=($posts_rs->Fields("ID"))?>" onClick="confirmit(); return false"> if it goes to delete.php and the ajax does not execute, then that means that there was a js error in confirmit(). Because there was a js error in that routine, the 'return false' following the js routine never executes and thereby the href= is followed. A js error in the routine is the only time the href= will be followed in a link of that format.(or js is turned off!) Make sure you have js error reporting turned on in your browser. Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-327173 Share on other sites More sharing options...
wazzoinc Posted September 27, 2007 Share Posted September 27, 2007 you still have the href set for the url. set it to javascript:void(0); Quote Link to comment https://forums.phpfreaks.com/topic/62700-help-with-ajax-and-php/#findComment-356091 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.