I've got a custom publish type, allows refer to it as items. Whenever a user drags the product towards the shopping cart software (droppable jQuery UI), I would like the important thing known as "amount" during my custom publish type to lessen by one.

To date I've got a JSON function via jQuery $.ajax that appears such as this:

$.ajax({ url: 'http://localhost:8888/MAMP/nogg/wordpress/wp-content/themes/twentyeleven/functions.php',
    data: { postid: +id },
    type: 'post',
    success: function(output) {
        alert("amount is reduced by 1.");
    }
});

This send the id from the publish to functions.php, i quickly make use of this to obtain the data during my functions.php

if(isset($_POST['postid']) && !empty($_POST['postid'])) {
    $postid = $_POST['postid'];
    $response = json_decode($postid);
    remove_amount($response);
}

Which calls the function using the postid.

function remove_amount($postid) {
    $amount = get_post_meta($postid, 'amount', true);
    update_post_meta($postid, 'amount', $amount--);
}

This provides us a 500 error, I have ensured it is the correct ID that's been sent, and checked the title from the area that contains the important thing (amount).

What exactly is my dumb self missing here?

You don't need to json_decode the $_POST['postid'] variable.

The [cde] method serializes your computer data object and transmits the information inside your request header as being a regular $.ajax. jQuery is not delivering JSON for your server. (You can improve your ajax parameters to really send JSON, however i wouldn't complicate your existence on the wordpress install. How you are utilizing POST is okay.)

Check it out such as this:

$.ajax

Also, what's using the if(isset($_POST['postid']) && !empty($_POST['postid'])) { // Make sure you do something in this function to prevent SQL injection. remove_amount($_POST['postid']); } inside your data object? Is the fact that intentional? Apart from that you will need to provide us with the PHP error that's leading to the HTTP 500.

get_publish_meta returns a string if you have set the $single param to true, that you've.

So, is the error associated with attempting to decrement a string value?

How about casting your amount val for an int before decrementing it?

+id

Does the road from the error message you are receiving (706) match the road where you stand your update meta?

Ok, I solved it. Apparently there is something within the Wordpress functions file that does not agree to handling json content like this. Hence not realizing the conventional Wordpress-functions (like get_publish_meta), things i did was to produce a blank page, been with them make use of a custom template using the php code, then within the jquery code I associated with that Wordpress-page.

function remove_amount($postid) {
    (int)$amount = get_post_meta($postid, 'amount', true);
    update_post_meta($postid, 'amount', $amount--);
}

and page_id=43 is really a page while using template below:

$.ajax({ url: 'http://localhost:8888/MAMP/nogg/wordpress/?page_id=43',
               data: {postid2: id },
               type: 'post',
               success: function(output) {
      }
});

The code runs because it should, now I simply need to do what Stephen stated and then add sql-injection protection. Just solutions! Got me within the right direction!