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 acplish.
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 acplish.
Share Improve this question edited Dec 24, 2012 at 22:45 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Jan 10, 2010 at 20:50 Brian T HannanBrian T Hannan 4,01519 gold badges59 silver badges100 bronze badges 6- May I direct you to this question: stackoverflow./questions/2034501/how-does-php-work/… – Tyler Carter 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. – James 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. – Brian T Hannan 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. – Brian T Hannan 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. – bmb Commented Jan 10, 2010 at 21:55
5 Answers
Reset to default 4Since 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.
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 code204 No Content
from PHP
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"]);
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./Ajax
All the normal security rules apply, i.e. make sure you filter ining 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.
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>