0

I found this code on the internet and i need the ajax code to POST the $row[ID] to my delete.php file:

echo "<td><a id=$row[ID]  onclick=\"if(confirm('Are you sure you want to delete this?')) deleteRow(this.parentNode.parentNode.rowIndex); return false; \" href=\"\"  >del</A> </td>"

I have tried:

function deleteRow(i){
    var makarios = document.getElementById('m').deleteRow(i)
    var mak;

    if (window.XMLHttpRequest) {
        // Mozilla, Safari, ... 
        mak = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // IE 8 and older
        mak = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var data = "id=" + makarios;
    mak.open("POST", "delete_basket.php", true);
    mak.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    mak.send(data);
}
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
Makis Theotokis
  • 27
  • 1
  • 1
  • 8
  • what have you tried? You may want to look into jQuery and the $.ajax method. It's pretty simple. – Kai Qing Apr 05 '13 at 01:55
  • Thanks for your fast reply , i am new into jquery i have tried function deleteRow(i){ var makarios = document.getElementById('m').deleteRow(i) var mak; if (window.XMLHttpRequest) { // Mozilla, Safari, ... mak = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE 8 and older mak = new ActiveXObject("Microsoft.XMLHTTP"); } var data = "id=" + makarios; mak.open("POST", "delete_basket.php", true); mak.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); mak.send(data); } but it did not work – Makis Theotokis Apr 05 '13 at 01:56

1 Answers1

0

In case you want a jQuery example that looks a lot easier than the jazz in your comment:

function deleteRow(id)
{
  $.ajax({
    type: "POST",
    url: "delete.php",
    data: { id: id },
    success: function(data){
      alert("deleted");
    }
  });
}

This makes the assumption that your delete script is looking for $_POST['id']

you do need to include the jQuery library. You can use a hosted one if you want: Here's a good resource for linking to hosted versions: Is there a link to the "latest" jQuery library on Google APIs?

I would go with 1.8 or so for now. using latest can lead to undesired side effects when major changes happen.

If you have a specific question feel free to ask and I may update.

Community
  • 1
  • 1
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • Didn't work, something goes wrong with the POST - Sorry for the mess in my comments , im new in here! – Makis Theotokis Apr 05 '13 at 02:16
  • you should be able to examine the network results in something like chrome developer tools. It would help to know what the response was. – Kai Qing Apr 05 '13 at 02:17
  • Please explain me what "data:{id:id}" stands for. Thanks again – Makis Theotokis Apr 05 '13 at 02:22
  • that is what the js is assigning as the $_POST array. id:id means assign key id to the value id... id was passed to the function. left of colon is key. right of colon is value. so if your passed id is 10, it would technically be sending id:10, which in php would be $_POST['id'] = 10 – Kai Qing Apr 05 '13 at 02:26