Peter
Peter

Reputation: 1296

PHP and Ajax post onclick events

I have a script like this:

<script type="text/javascript">
function DeletePublisher(publisherid) {
jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function(r) { if (r)

scriptitem = document.createElement('script');
scriptitem.type = 'text/javascript';
scriptitem.src = 'includes/publishers/delete-publisher.php?publisherid=' + publisherid;
scriptitem.id = 'ajax';
document.body.appendChild(scriptitem);
setTimeout('document.body.removeChild(document.getElementById("ajax"))', 500);  

$.jGrowl('Publisher deleted');
window.location.reload();
});
}
</script>

And I am listing rows in table like this:

TD ROWS HERE...
<td class="unpaid-th"><strong><?php echo $publisher_unpaid; ?></strong></td>
                <td class="action-th">
                    <ul class="button-table-head">
                        <li><div class="button-head edit-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Edit" data-style-tooltip="tooltip-mini-slick"><span>Edit</span></a></div></li>
                        <li><div class="button-head delete-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Delete" data-style-tooltip="tooltip-mini-slick" onclick="DeletePublisher('<?php echo $publisher_id; ?>')"><span>Delete</span></a></div></li>
                    </ul>
                </td>

Now what should happen is when I click on delete link - it should post/get (whatever - as i use $_REQUEST on listener script) to php script that will get the id of publisher and delete it from DB.... But the problem seems to be that no ID is actually send to delete script and I've tried everything....

It displays nice in source code like onclick="DeletePublisher('152')" and prompts alerts, infos etc... but it seems that it's not sending ID..... OR NOW MAYBE NOT EVEN CALLING A LISTENER script (don't know how to test it) :(

Any ideas what's wrong here (or maybe different approach?) ?

Thanks a lot !

Upvotes: 0

Views: 1322

Answers (2)

Muthu Kumaran
Muthu Kumaran

Reputation: 17900

I would recommend using AJAX like below which uses jQuery

function DeletePublisher(publisherid) {
  jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function(r) { if (r)
    $.ajax({
      type: "POST", //or GET
      url: 'includes/publishers/delete-publisher.php?publisherid=' + publisherid,
      data: '',
      success: function(response){
        $.jGrowl('Publisher deleted');
        window.location.reload();
      }
    });
  });
}

Upvotes: 1

Subir Kumar Sao
Subir Kumar Sao

Reputation: 8401

I think what you are trying to do is JSONP call. What I think would be missing is that the response should be wrapped inside a javascript call. The name of the method should be same as the callback method.

But I think you need not go ahead with JSNOP aproach as I see your script is on the same domain. So try making a simple ajax call. You can try jquery. It will make take care of browser compatibility.

Upvotes: 0

Related Questions