I am using this jQuery plugin to render a toggle button in a block. When the button is toggled off/on I want to write to a databse table (with entity_save
). Here's the relevant code I'm using to try to acheive this:
// hook_menu item
$items['product-status-update'] = array(
'title'=>'Status update',
'page callback' => 'my_module_status_update',
'access callback' => 'product_entity_access',
'type'=> MENU_CALLBACK,
);
// jQuery
drupal_add_js('
(function($) {
$(function() {
$(".toggle-button").toggles({' . $status . $state . '});
$(".toggle-button").on("toggle", function (e, status) {
status = status == true ? 1 : 0
$.ajax({
type: "POST",
url: "?q=product-status-update",
data: "pid="+' . $pid . '&"status="+status,
});
});
});
})(jQuery);
', 'inline');
// Callback
function my_module_status_update() {
$pid = $_POST['pid'];
$status = $_POST['status'];
$product = entity_load_single('product', $pid);
$product->status = $status;
entity_save('product', $product);
}
// Console log output when toggling
POST http://example.com/?q=product-status-update 500 (Internal Server Error) jquery-1.10.2.js:8706
1 ?q=home:98
0 ?q=home:99
POST http://example.com/?q=product-status-update 500 (Internal Server Error) jquery-1.10.2.js:8706
1 ?q=home:98
1 ?q=home:99
POST http://example.com/?q=product-status-update 500 (Internal Server Error) jquery-1.10.2.js:8706
1 ?q=home:98
0 ?q=home:99
As you can see from the console log output I am receiving: 500 (Internal Server Error).
Any ideas why or what might be the culprit? Thanks.
entity_load_single
is returning false - because $_POST, and therefore $pid, is empty. So I gues my issue is with my implementation of$.ajax
in my jQuery.