1

I need to call the gh_cart_prove when the BUTTON is clicked.

function gh_view_categories(){
  $a='<table>';
  $a.='<tr>';
  $a.='<td><strong>'.'Categoria:'.'</strong></td>';
  $a.='<td><strong>'.'Soglia minima:'.'</strong></td>';
  $a.='<td><strong>'.'Tot attuale:'.'</strong></td>';
  $a.='<td><strong>'.'Crea ordine'.'</strong></td>';
  $a.='</tr>';
  $query = db_select('uc_product_classes', 'u')
    ->fields('u', array('name','soglia', 'totattuale'));
  $result = $query->execute();
  while ($record = $result->fetchAssoc()) {
    $idUser=gh_get_user_id($record['name']);
    $a.='<tr>';
    $a.='<td>'.$record['name'].'</td>';
    $a.='<td>'.$record['soglia'].'</td>';
    $a.='<td>'.$record['totattuale'].'</td>';
    $a.='<td>'.'<button onClick="gh_cart_prove($idUser)">BUTTON</button>'.'</td>';
    $a.='</tr>';

  }

  $a.='</table>';

  return ($a);
}

The button appear but it not works. the function is correctly declared. How can I resolve this problem?

4
  • 1
    Welcome to Drupal Answers! Is gh_cart_prove() a JavaScript function?
    – apaderno
    Sep 28, 2015 at 16:04
  • no it isn't.... Sep 28, 2015 at 16:09
  • 1
    if gh_cart_prove is NOT a JS, it may help to get an answer if you EDIT your question to explain what it IS ... Sep 28, 2015 at 17:18
  • I would assume that you have gh_cart_prove() as a PHP function in a contributed module? What I don't see here is a <form> element. If you want to handle it like a form, I would recommend placing one in your table (even if it's within your existing <td>). Otherwise, Javascript would be your best approach. Sep 28, 2015 at 21:53

1 Answer 1

1

Assign an id to the button and use Jquery, set the $idUser in a data attribute example :

 <button id="buddon-id" data-user="<?php echo $idUser;?>" >button</button>
    <script>
    $("#button-id").on("click", function(e){
       userid = $(this).attr("data-user");
    };
    </script>
2
  • Hi Riccardo, thanks a lot.. I'm new with jquery, Where do I put the code of the script? in what way? Sep 29, 2015 at 7:34
  • I suggest you to create a js file and insert the path in the yourthemename.info file, install jquery update module and add the code after $ = JQuery; jQuery(document).ready(function() { $ = jQuery; //.... your code here }); Sep 29, 2015 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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