views:

789

answers:

5

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.

+1  A: 

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"]);
Marek Karbarz
I am familiar with what ajax does, but I am not familiar with the implementation details. Any insight?
Brian T Hannan
I'd try googling "jquery ajax tutorial" for some starting material
Erik
You create an XMLHTTPRequest object in JavaScript, set some parameters, and fire off the request. Google has some *great* tutorials, try looking there :).
chpwn
+1  A: 

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
Christoph
Hey, the second one is clever. Never heard of that. Does the original page still work after you do that?
Pekka
@Pekka: yes, it should; see http://benramsey.com/archives/http-status-204-no-content-and-205-reset-content/ for a discussion of status codes 204 (which is widely supported) and 205 (which isn't)
Christoph
+2  A: 

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.

bmb
A: 

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>
Joel L
+1  A: 

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.

simonrjones