0

I am working on jQuery/AJAX code as shown below which calls convert.php script.

   jQuery(document).ready(function($)
   {
    $('.go-btn').click(function()
    {   
        let target = $(this).attr('data-id'),
            spanEl = $('.file-name[data-id='+ target +']');

        $.ajax({
            url: 'convert.php',
            type: 'POST',
            data: {id: target}, //change to what you want
            success: function(res)
            {
                console.log("Tested");
            },
            error: function(res)
            {

            }
        })
    })  
   });  

In convert.php script conversion of mp4 into mp3 is happening. Once the conversion is complete, on console Tested is printed.

Problem Statement:

I am wondering what changes I should make in jQuery/AJAX code above so that once the conversion is complete, the button text belonging to the HTML code below gets change to Completed

The above jQuery/AJAX code is called on button click. Here is the snippets of HTML code belonging to the button:

HTML Code:

  <?php  foreach ($programs as $key => $program) {  ?> 
       <tr data-index="<?php echo $key; ?>">
          <td><input type="submit" name="go-button" value="Go" data-id="<?php echo $key; ?>" ></input></td>
       </tr>
  <?php }?>
5
  • 1
    Why not just update the button text with jQuery with success: $('.go-btn').val('Completed'); Commented Jun 13, 2019 at 18:54
  • Where do you want me to update this ? Commented Jun 13, 2019 at 18:55
  • Within success - same place as you are writing "tested" to the console. Commented Jun 13, 2019 at 18:57
  • like this ? success: function(res) { $('.go-btn').val('Completed'); }, Commented Jun 13, 2019 at 18:58
  • It worked, thanks for the help @MER I have one small question. The above HTML code has many rows and each single row has Go button. Once the process is complete for individual row, Go text gets changed to Completed in all rows which is not I want. Go text should get changed to completed only to the row which its belong to. Commented Jun 13, 2019 at 19:10

1 Answer 1

1

It's just a matter of capturing the button that was clicked:

jQuery(document).ready(function($) {
  $('.go-btn').click(function() {
    let target = $(this).attr('data-id'),
      spanEl = $('.file-name[data-id=' + target + ']');
    // Capture the element that received the event
    let btn = this;
    $(btn).val("Converting").prop('disabled', true).css({
      'text-align': 'center',
      'border-color': '#000',
      'width': '120px'
    });
    $.ajax({
      url: 'https://api.myjson.com/bins/80dex',
      method: 'GET', // Change this back to POST
      data: {
        id: target
      }, //change to what you want
      success: function(res) {
        $(btn).val("Completed")
      },
      error: function(res) {
        $(btn).prop('disabled', false).val("Go").removeAttr('style');
      }
    })
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<tr data-index="1">
  <td>
    <input type="submit" name="go-button" class="go-btn" value="Go" data-id="1"></input>
  </td>
</tr>
<tr data-index="2">
  <td>
    <input type="submit" name="go-button" class="go-btn" value="Go" data-id="2"></input>
  </td>
</tr>
<tr data-index="3">
  <td>
    <input type="submit" name="go-button" class="go-btn" value="Go" data-id="3"></input>
  </td>
</tr>

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

8 Comments

Why did you add let btn = this; ?
@john Because it's the specific button that was clicked. If you tried to use this inside the callback, this would point to the request. Javascript is weird like that. You can learn more in this article or this book
Make sense and we can use both GET and POST
@john you can do it in the same handler, I updated the answer
@john what if you took care of that in the php side by disabling the buttons beforehand? If you are looking for realtime updates, I'm thinking you might use WebSockets instead. Hard to say without knowing more about your architecture.
|

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.