Brian T Hannan
Brian T Hannan

Reputation: 4005

How do I call a php function from javascript onclick() event?

For example, I have a php function:

function DeleteItem($item_id)
{
     db_query("DELETE FROM {items} WHERE id=$item_id");
}

Then, I have something like the following:

<html>
<head>
<script type="text/javascript">
function DeleteItem($item_id)
{
     alert("You have deleted item #"+$item_id);
}
</script>
</head>

<body>
<form>
Item 1
<input type="button" value="Delete" onclick="DeleteItem(1)" />
</form>
</body>
</html>

I want to be able to call the PHP function DeleteItem() from the javascript function DeleteItem() so that I can use Drupal's db_query() function, so I don't have to try to establish a connection to the database from javascript.

Does anyone have any suggestions on how this might be done? P.S. I understand that PHP processes on the server-side and javascript processes on the client-side, so please no responses saying that. There has got to be some kind of trick one can do in order to have this work out. Or maybe there is a better way of doing what I am trying to accomplish.

Upvotes: 1

Views: 23181

Answers (5)

simonrjones
simonrjones

Reputation: 1182

you can't actually call PHP functions within JavaScript per se. As @Christoph writes you need to call a PHP script via a normal HTTP request from within JavaScript using the magic that is known as AJAX (silly acronym, basically means JS can load external HTTP requests on the fly).

Take a look at jQuery's AJAX functionality on how to reliably make a HTTP request via JS, see http://docs.jquery.com/Ajax

All the normal security rules apply, i.e. make sure you filter incoming data and ensure it's what you're expecting (the $item_id in your example). Bear in mind there's nothing to stop someone manually accessing the URL requested by your JS.

Upvotes: 2

Marek Karbarz
Marek Karbarz

Reputation: 29294

You will want to use ajax for that. Also, database connection from within javascript is something you should not even consider as an option - terribly insecure.

A very simple example:

//in javascript
function DeleteItem($item_id) {
    $.post("delete.php", { id: $item_id}, function(data) {
        alert("You have deleted item #"+$item_id);
    });
}

//in php file
db_query("DELETE FROM {items} WHERE id=" . $_REQUEST["id"]);

Upvotes: 2

Joel
Joel

Reputation: 3060

First, use jQuery.

Then, your code will have to be something like:

<input ... id="item_id_1" />

<script>
$(document).ready(function() {
   $('input').click(function(){
      var item_id = $(this).attr('id');
      item_id = item_id.split('_id_');
      item_id = item_id[1];
      $.get('/your_delete_url/' + item_id);
   });
});
</script>

Upvotes: 0

bmb
bmb

Reputation: 6248

Since you are aware that PHP processes on the server-side and javascript processes on the client-side, you must also realize you can't call a PHP "function" from javascript. Your client side code can redirect to a PHP page, or invoke a PHP program using AJAX. That page or program must be on the server and it should do a lot more than just the one line you have in your function. It should also check for authentication, authorization, etc. You don't want just any client side script anywhere to call your PHP.

Upvotes: 4

Christoph
Christoph

Reputation: 169543

You need to write a PHP script which will execute the function. To call it, either:

  • use XMLHttpRequest (aka Ajax) to send the request
  • change the page's location.href and return HTTP status code 204 No Content from PHP

Upvotes: 2

Related Questions