0

I have a table with 7 columns and i have a php code that is used to fill up the table it is:

while($ad_table = mysqli_fetch_assoc($result)){
   if(isset($ad_table['status'])){
       if($ad_table['status'] == '0'){
           $status = "paused";
       }else if($ad_table['status'] == '1'){
           $status = "users are viewing your site";
       }
   }
   $no = $no + 1;
   echo "<tr><td>{$no}</td><td>{$ad_table['name']}</td><td>{$ad_table['site']}</td><td>{$ad_table['hits']}</td><td id='status'>{$status}</td><td ><input type='text' name='id_ad' id='id{$no}' value='{$ad_table['id']}' /><td><div class='btn-group'><button type='button' class='btn btn-info' id='pause' value='pause{$no}' ><i class='fa fa-pause'></i> pause </button><button type='button' class='btn btn-info' id='edit' ><i class='fa fa-edit'></i> edit </button><button type='button' class='btn btn-danger' id='delete' ><i class='fa fa-delete'></i> delete </button></div></td></tr>";
 }
 } ?>

now the table will have a pause button,edit,delete button.i want the buttons to work with ajax jquery.But the problem is how to get the $ad_table['ad_id'] which is used to pause the ad with that id.Can anyone help me with this?I already lost a lot of time working with this.

1
  • Can u post your current jQuery? Commented Feb 13, 2015 at 10:28

2 Answers 2

2

As far as i understand your question, u need to use jQuerys .on() and .data() methods.

For Example:

You give your buttons a data attribute like this:

<button class="pause" data-id="{$ad_table['ad_id']}">Click me</button>

And your jQuery would look like this:

$(".pause").on("click", function() {
    // Request
    $.ajax({
        url: "/myAjaxFolder/myServerSideProcess.php?id="+ $(this).data("id");
    })

    // Success
    .done(function(data) {

    })

    // Error
    .fail(function() {

    });
});

Hope this helps.

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

Comments

0

You can use HTML5 data attributes to embed your ad_id in the buttons.

When you have so much amount of html, your code becomes more readable if you separate html and php. And you cannot have elements with same "id", always try to use class in loops. If you need to use "id" append the $ad_id or $no to make sure it is unique. For example I did it for status column below.

<?php
while($ad_table = mysqli_fetch_assoc($result)){
    if(isset($ad_table['status'])){
       if($ad_table['status'] == '0'){
           $status = "paused";
       }else if($ad_table['status'] == '1'){
           $status = "users are viewing your site";
       }
    }
    $no = $no + 1;

    ?>
    <tr>
        <td><?php echo $no; ?></td>
        <td><?php echo $ad_table['name']; ?></td>
        <td><?php echo $ad_table['site']; ?></td>
        <td><?php $ad_table['hits']; ?></td>
        <td id='status-<?php echo $ad_table['ad_id']; ?>'><?php echo $status; ?></td>
        <td>
            <input type='text' name='id_ad' id='id<?php echo $no; ?>' value='<?php echo $ad_table['id']; ?>' />
        </td>
        <td>        
            <button type='button' class='btn btn-info btn-pause' data-ad-id="<?php echo $ad_table['ad_id']; ?>"><i class='fa fa-pause'></i> pause </button>
            <button type='button' class='btn btn-info btn-edit' data-ad-id="<?php echo $ad_table['ad_id']; ?>"><i class='fa fa-edit'></i> edit </button>
            <button type='button' class='btn btn-danger btn-danger' data-ad-id="<?php echo $ad_table['ad_id']; ?>"><i class='fa fa-delete'></i> delete </button>
        </td>
    </tr>
<?php
}

In your javascript, you can use the jQuery.data() method to retrieve the ad_id.

$('.btn-pause').on('click', function() {
    var ad_id = $(this).data('ad-id');
    // do ajax request using ad_id
});

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.