0

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.

4
  • 1
    Http 500 really means: check error logs. Have you seen them? what's there? Both in your webserver's error.log and Drupal's watchdog.
    – Mołot
    Oct 13, 2014 at 7:49
  • Thanks. I have a database query error in watchdog. I was looking for a js/server error, not a Drupal-php error. Looking in the wrong place.
    – dbj44
    Oct 13, 2014 at 8:19
  • Put that database query error in your question if you can't fix it by yourself. Or consider posting self-answer about proper logs and final solution.
    – Mołot
    Oct 13, 2014 at 8:21
  • My database query was failing because 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.
    – dbj44
    Oct 13, 2014 at 9:45

2 Answers 2

0

At a glance, there's some issues with the code, however, the main error may be:

$product = entity_load_single('product', $pid);

where you don't have a $pid value, you're setting $fid instead.

$fid = $_POST['pid'];
1
  • Sorry, that was a typo, but I've identified the problem: $_POST is empty.
    – dbj44
    Oct 13, 2014 at 9:41
0

My mistake was:

data: "pid="+' . $pid . '&"status="+status,

should be

data: "&pid=" + ' . $pid . ' + "&status=" + status,

Seems obvious now.

Anyway, the above is the basics of how to update a value in the database by sliding a jQuery toggle button back and forth.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.