0

I have a system which currently outputs a table of results in html for the browser.

These results can be updated by clicking the approveCredit button on each row.

I'd like to create a javascript catch which looks out for the button click and passes the given variable from the row and performs an ajax task.

Code:

PHP to echo table of results

<tbody>
 <?php 
 $count = 1;
   while ($row = mysql_fetch_array($test))  
 {?>

 <tr>
    <td class="text-center"><?php echo $count++;?></td>
    <td><?php echo $row['user_id'];?></td>
    <td><?php echo $row['credits'];?></td>
    <td><?php echo $row['notes'];?></td>
    <td><?php echo $row['po'];?></td>
    <td><?php echo $row['date_received'];?></td>
    <td class="text-center">
        <div class="btn-group">
            <a class="btn btn-xs btn-default approveCredit" type="button" value="<?php echo $row['credits'];?>" data-toggle="tooltip" data-original-title="Approve Credit Request"><i class="fa fa-check"></i></a>
        </div>
    </td>
  </tr> 
  <?php } ?>    
  </tbody>

Javascript Awaiting Button Click

$('.approveCredit').on('click', function() {

        var creditid = $('.approveCredit').val();

        $.ajax({
            url: "ajax url to update database",
            type: "POST",
            data: {creditid: creditid},
            success: successFunction,
            error: errorFunction
        });

});
6
  • That's nice. What's your question? Commented Sep 29, 2015 at 9:35
  • 2
    Try this: var creditid = $(this).attr('val'); Commented Sep 29, 2015 at 9:38
  • apologies, it doesn't work @JonStirling! Commented Sep 29, 2015 at 9:43
  • Define "doesn't work" . What bit doesn't work (even if we can guess), what does it give you, what were you expecting etc etc. All useful information. Commented Sep 29, 2015 at 9:44
  • Seems my problem has been solved, thanks to Sajitha Nilan. Commented Sep 29, 2015 at 9:45

1 Answer 1

2

Try this. actually you must use Jquery attr to get value from the ancher

<a class="btn btn-xs btn-default approveCredit" type="button" value="<?php echo $row['credits'];?>" data-toggle="tooltip" data-original-title="Approve Credit Request"><i class="fa fa-check"></i></a>

value is an attribute of the ancher. So you should use it like this,

var creditid = $('.approveCredit').attr('value');

and also preventing default action of ancher (a tag) change your code as this

$('.approveCredit').on('click', function(e) {
        e.preventDefault();
        var creditid = $('.approveCredit').attr('value');

        $.ajax({
            url: "ajax url to update database",
            type: "POST",
            data: {creditid: creditid},
            success: successFunction,
            error: errorFunction
        });

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.