1

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.

6
  • May I direct you to this question: stackoverflow.com/questions/2034501/how-does-php-work/… Commented Jan 10, 2010 at 20:59
  • 3
    Are you sure you want to do this? You're enabling any client, anywhere, to delete ANY item from your database. Not a good idea. Commented Jan 10, 2010 at 21:00
  • Actually, I really don't WANT to do this, but I'm having problems doing what I'm what I'm trying to do. Drupal has 'Panels' and 'Tabs' modules. You can put tabs in your panel for all or some of the content within your panel. However, there is no way to direct the user to a specific tab via a link or call. Commented Jan 10, 2010 at 21:11
  • 1
    So if I want to delete or especially edit an item from a list within the the 3rd tab I CAN do it but when I make the call to the page and provide the parameters in the URL it shows me the first tab when I reload the page with the parameters instead of the tab I want. This makes it so the user has to click on that 3rd tab again, which is annoying for them. Commented Jan 10, 2010 at 21:12
  • Maybe a better question is "how do I access server-side functionality from a web page without a page-reload in the browser?" AJAX is perfect for this. Commented Jan 10, 2010 at 21:55

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

Hey, the second one is clever. Never heard of that. Does the original page still work after you do that?
@Pekka: yes, it should; see benramsey.com/archives/… for a discussion of status codes 204 (which is widely supported) and 205 (which isn't)
2

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"]);

3 Comments

I am familiar with what ajax does, but I am not familiar with the implementation details. Any insight?
I'd try googling "jquery ajax tutorial" for some starting material
You create an XMLHTTPRequest object in JavaScript, set some parameters, and fire off the request. Google has some great tutorials, try looking there :).
2

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.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.