Hi, this is what im trying to do:

I made an inventory management, that was easy, I just did the content type product with its respective fields of cuantity, and others.
In a view I did 2 tables the normal stock and the critic stock, where I show the name of the product, the quantity and 2 buttons for each product, the button (+) and the button (-) those buttons are for add or remove products of the stock, you understand, this is an example

name | quantity | actions
product 1 | 5 | (+) (-)
product 2 | 10 | (+) (-)
etc...............

Well if you click the (+) or (-) buttons, in the quantity column must add or remove items depending the button you clicked.

Well i could do this but not as wanted, I want to implement ajax...

Well I did a custom module called ajax_inventory

this is the ajax_inventory.module file


/**
 * 
 * Implementing hook_menu()
 * 
 **/
 
 function ajax_inventory_menu(){
    
    $items = array();
    
    $items['inventory/plus/%'] = array(
    'type' => MENU_CALLBACK,
    'page callback' => 'ajax_add_inventory',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    );
    
    $items['inventory/minus/%'] = array(
    'type' => MENU_CALLBACK,
    'page callback' => 'ajax_less_inventory',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    );
    return $items;
 }
 /**
  * 
  * When the button + is clicked
  * @param the node id or product id
  *
  **/
 function ajax_add_inventory($nid){
  $node = node_load($nid);
  $node->field_quantity[$node->language][0]['value'] +=1;
  node_save($node);
  drupal_goto("inventario");
    
 }

  /**
  * 
  * when the button - is clicked
  * @param the node id or product id
  *
  **/ 
 function ajax_less_inventory($nid){
  $node = node_load($nid);
  $node->field_quantity[$node->language][0]['value'] -=1;
  node_save($node);
  drupal_goto("inventory");
    
 } 

well how you see I use 2 paths for the buttons, the +/- buttons are not buttons, they are links

the + link (or button whatever) links to the inventory/plus/%
and the - link links to inventory/minus/%

where % is the node id, then i can catch the node object and edit the quantity field.

Ho can I make this with ajax?? can someone tell me the way forward, I really does not understand well the ajax framework of drupal.

Well I do not want to use the drupal_goto() function, I do not want to reload the page if you click the +/- buttons

Thanks for the help.

Comments

findmashmind’s picture

You can use Drupal ajax API. Please check following link https://api.drupal.org/api/drupal/includes!ajax.inc/function/ajax_comman...