almightyegg Posted July 30, 2007 Share Posted July 30, 2007 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... Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted July 30, 2007 Share Posted July 30, 2007 is t and f on the delete.php page or on the main page? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted July 30, 2007 Share Posted July 30, 2007 don't you just mean pass the php variables to javascript? example <script> var t=<?php echo($t);?> var f=<?php echo($f);?> </script> Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 I don't know lol, I'm a complete javascript and ajax newbie... Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 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? Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 Okay, what are you making the users do to select which one they want to delete? It does not have to be a full path. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 Well, to delete a message you have to be viewing it, so the link will be viewthread.php?t=$t&f=$f the user just clicks the button that says delete and it gets the $t and $f and sends it to delete.php Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 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? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 Sort of, I don't know how to put my information into gup() Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 so what goes here? <input type="button" value="Delete" id="sendButton" onClick="sendRequest('VARIABLES HERE');" /> because that's all changed now... Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 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!' } } Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 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> Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 Deleted that bit of code and added in that alert and it showed up nothing... yet it doesn't work ??? how confusing Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 Okay 2 things, Does it actually delete it from the DB? What does the alert show? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 30, 2007 Author Share Posted July 30, 2007 1. Nope 2. The pop-up? It asks whether you want to delete it and when you click ok an alert icon appears in the browser bottom left hand corner. If you mean the alert(url) then it doens't show anything Quote Link to comment Share on other sites More sharing options...
Philip Posted July 30, 2007 Share Posted July 30, 2007 <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 Quote Link to comment Share on other sites More sharing options...
Philip Posted July 31, 2007 Share Posted July 31, 2007 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> Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 31, 2007 Author Share Posted July 31, 2007 right....It alerted out delete.php?t=5&f=gdis which is correct but failed to delete.... Quote Link to comment Share on other sites More sharing options...
Philip Posted July 31, 2007 Share Posted July 31, 2007 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 } ?> Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 31, 2007 Author Share Posted July 31, 2007 Ahh! I spotted an error on the alert(url) there is no & sign. It should be delete.php?t=$t&f=$f 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.