Jump to content

Help with AJAX and PHP


ameriblog

Recommended Posts

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...

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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);])

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

  • 2 weeks later...

<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.

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.