Jump to content

[SOLVED] Question


almightyegg

Recommended Posts

I have 2 PHP variables t and f, how would I put 2 variables into this function?

function sendRequest(???) { 

   // Open PHP script for requests 
   http.abort; 
   http.open('post', 'delete.php'); 
   http.setRequestHeader('Content-Type', 'application/x-www-form- 
      urlencoded'); 
   http.send(????); 

 

I'm a complete newb to ajax, and I know this is very simple but I just want a nice easy script to delete posts...

Link to comment
Share on other sites

Well, the page where all of this is viewed is viewthread.php?t=$t&f=$f I want the ajax script to send those values to delete.php, making it a link like delete.php?t=$t&f=$f where then it will get the f and t to run the script:

delete.php

<?
include 'db.php'; // Connect to DB
$id = $_GET['t'];
$f = $_GET['f'];
$deletepost = mysql_query("DELETE FROM topics WHERE id = '$id' and fid = '$f' LIMIT 1");
$delpos = mysql_query($deletepost);

?>

 

 

Link to comment
Share on other sites

I'll post all the relevant codes ~

viewthread.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="default.css"> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Viewing Post</title>
<script type="text/javascript">
if (window.XMLHttpRequest)     // Object of the current windows
{ 
    xhr = new XMLHttpRequest();     // Firefox, Safari, ...
} 
else 
if (window.ActiveXObject)   // ActiveX version
{
    xhr = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
} 

xhr.onreadystatechange = function() { // instructions to process the response }; 

if (xhr.readyState == 4)
{
   // Received, OK
} else 
{
  // Wait...
}

xhr.open('GET', 'http://www.lordoftheabyss.com/forum/delete.php', true);                  
xhr.send(null); 


function sendRequest(t) { 

   // Open PHP script for requests 
   http.abort; 
   http.open('post', 'delete.php'); 
   http.setRequestHeader('Content-Type', 'application/x-www-form- 
      urlencoded'); 
   http.send('t='+t); 

}
function sendRequest2(f) { 

   // Open PHP script for requests 
   http.abort; 
   http.open('post', 'delete.php'); 
   http.setRequestHeader('Content-Type', 'application/x-www-form- 
      urlencoded'); 
   http.send('f='+f); 

}
function handleResponse() {

   if(http.readyState == 4 && http.status == 200){

      // Text returned from PHP script
      var response = http.responseText;

      if(response) {
         // Update ajaxTest content
         document.getElementById("ajaxTest").innerHTML = response;
      }

   }

}

</script>
</head>

<body>
<?
$t = $_GET['t'];
$f = $_GET['f'];
// some irrelevant code
?>
<input type="button" value="Delete" id="sendButton" onClick="sendRequest('VARIABLES HERE');" />
</body>
</html>

 

Delete.php

<?
include 'db.php';
$id = $_GET['t'];
$f = $_GET['f'];
$deletepost = mysql_query("DELETE FROM topics WHERE id = '$id' and fid = '$f' LIMIT 1");
$delpos = mysql_query($deletepost);

?>

 

What I want it to do is to send the variables, defined in the link viewthread.php?t=X&f=Y , to the delete.php script via the ajax without reloading the page, and although I've not done it yet, I would like it to have a confirmation pop-up box....

 

It doesn't do any of this but shows an alert in the bottom left corner saying Error on page

Link to comment
Share on other sites

Use it in GET instead of POST in your JS.

 

Here's a sample of some of my coding that you can change up:

function 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) {
	textr = "Deleting...";
	var t = document.getElementById("t_value_box").value;
	var f = document.getElementById("f_value_box").value;	
	var url = "delete.php?f=" + f + "t=" + t;		
	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
	}
}
}

 

That should be a good start for you, as it was for me with my AJAX script.

Link to comment
Share on other sites

Cheers for the reply, I have put it into nthe <head> section within the <script> tags etc... and put in the url (i think !?!?) but I don't know how to put in that I want it to send the values of $t and $f to the delete.php file....

 

and does it need the full path of the URL or as it is in the same folder can I just put delete.php?

Link to comment
Share on other sites

Okay,

 

since JS doesn't give any easy way (that I know of) to get variables from the URL, I've found a function that can do it.

 

http://www.netlobo.com/url_query_string_javascript.html (and there are some disadvantages, read the comments)

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

 

You would then put this into the JS and make t and f run the function:

var t = gup(t);
var f = gup(f);
var url = "delete.php?f=" + f + "t=" + t;		
sendRequest(request, url);
...

 

Does that make sense?

Link to comment
Share on other sites

You're not going to need to change gup at all. It does all the work for you. All you have to do is tell it what variable name you want to get out of the URL (which is where the gup(t) comes into play)

 

 

For example:

The url is /viewthread.php?t=100&f=999

 

In JS:

var t = gup(t);

// t becomes 100, if the url above is used

var f = gup(f);

// f becomes 999, if the url above is used

Link to comment
Share on other sites

Okay:

 

<input type="button" value="Delete" id="sendButton" onClick="confirmit();" />

 

And for within the JS:

function confirmit() {
input_box=confirm("Are you sure you want to delete this item?");
if (input_box==true) {
	var t = gup(t);
	var f = gup(f);	
	var url = "delete.php?f=" + f + "t=" + t;		
	sendRequest(request, url)
} else {
	//Abort part (I changed the text on page displaying 'aborted!'
}
}

 

Link to comment
Share on other sites

Right, I clicked delete and the pop-up came up and I clicked ok, refreshed but it hadn't deleted....

<script type="text/javascript">
function 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 gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var t = gup(t);
var f = gup(f);
var url = "delete.php?f=" + f + "t=" + t;		

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 t = gup(t);
	var f = gup(f);	
	var url = "delete.php?f=" + f + "t=" + t;		
	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>

Link to comment
Share on other sites

Okay, lets do some troubleshooting:

 

add in this right before sendRequest(request,url);

 

alert(url);

 

 

Also, you have this right after the AJAX request is made:

 

 

var t = gup(t);

var f = gup(f);

var url = "delete.php?f=" + f + "t=" + t;

 

function sendRequest(request, url) {

..

 

 

Go ahead and delete it, you'll only need it when you run confirmit

Link to comment
Share on other sites

<script type="text/javascript">
function 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 gup(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}		

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 t = gup(t);
	var f = gup(f);	
	var url = "delete.php?f=" + f + "t=" + t;	
	alert(url);
	// Should display "delete.php?...."
	sendRequest(request, url);
} else {
	//Abort part (I changed the text on page displaying 'aborted!'
	// Does not have to be anything
}
}
function finishUpdate() {
if (request.readyState == 4) {
	if (request.status == 200) {
		//do whatever when PHP script is done
		//perhaps refresh the page
		//or change the text on the page. whatever you want
	}
}
}
</script>

 

Make sure the script is the same as above. It should alert at least "delete.php?t=" on it

Link to comment
Share on other sites

Okay I found the problem. The function I found earlier that got the URL parameter, wasn't working properly. I replaced it with another one, and I tested the following script (which works):

 

<html>
<head>
<script type="text/javascript">
function 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 gup(strParamName){
 var strReturn = "";
 var strHref = window.location.href;
 if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
    {
		if (aQueryString[iParam].indexOf(strParamName + "=") > -1 )
		{
			var aParam = aQueryString[iParam].split("=");
			strReturn = aParam[1];
			break;
		}
    }
 }
 return strReturn;
} 		

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 t = gup('f');
	var f = gup('t');	
	var url = "delete.php?f=" + f + "t=" + t;	
	alert(url);
	// Should display "delete.php?...."
	sendRequest(request, url);
} else {
	//Abort part (I changed the text on page displaying 'aborted!'
	// Does not have to be anything
}
}
function finishUpdate() {
if (request.readyState == 4) {
	if (request.status == 200) {
		//do whatever when PHP script is done
		//perhaps refresh the page
		//or change the text on the page. whatever you want
	}
}
}
</script>

</head>
<body>
<a href="#" onclick="confirmit(); return false">delete me</a>
</body>
</html>

Link to comment
Share on other sites

Okay, also in your PHP script, you need to have some way for AJAX to know if the deletion went through, a simple 1/0 will do fine.

 

I'm not sure if this is causing the problem or not.

 

<?
include 'db.php';
$id = $_GET['t'];
$f = $_GET['f'];
$deletepost = mysql_query("DELETE FROM topics WHERE id = '$id' and fid = '$f' LIMIT 1");
$delpos = mysql_query($deletepost);
if($delpos) {
echo '0' // no error
} else {
echo '1' // error
}
?>

Link to comment
Share on other sites

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.