skip to Main Content

From the database, I have a dynamic table like this:

<table>
    <?php
    $query = ....;
    foreach ($query as $row) {
        echo '<tr>';
        echo '<td>' . ' <span class="bName">' . $row['brand_name'] . '</span>'.
        '<input name="product_id" type="number" value="' . $row['product_id'] . '">'.
        '<input name="company_id[]" type="number" value="' . $row['company_id'] . '">'.
        '<button name="exchange" type="button">Click Me!</button></td>';
        echo '</td>';
        echo '</tr>';
    }
    ?>
</table>

It returns say 4 rows with brand_name inside the <span> and product_id inside an <input>. The exchange button on click calls an ajax request that query another random brand_name and returns the query as JSON like this:

{product_id: '2206', brand_name: 'New name', company_id: '234' }

The script for ajax is

<script>
    $(document).ready(function() {
        $('button[name="exchange"]').click(function() {
            $.ajax({
                url: 'ajaxChangeBrand.php',
                type: 'POST',
                data: 'keyword=' + $(this).parent().find('input[name="product_id"]').val(),
                success: function(response) {
                    var data = JSON.parse(response);
                    $('.bName').html(data.brand_name); // Problem is here
                    $('.company_id').html(data.company_id); // and here
                    console.log(data);
                },
            });
        });
    });
</script>

My target is to change the brand_name inside class bName and company_id value with the new values from ajax response for that specific row. But my problem is it changes all the spans with bName class and all the inputs with class company_id. What should be the best approach to change the specific row of that table from the ajax data?

2

Answers


  1. Store a reference to the cell that the button that was actually clicked exists in so you can find within that cell the specific elements.

    Also note that the company_id value is in an input thaat you ned to use val() on and you need to give it a class name

    $('button[name="exchange"]').click(function() {
      // cell this button instance is in
      const $cell = $(this).closest('td');
    
      $.ajax({
        url: 'ajaxChangeBrand.php',
        type: 'POST',
        data: 'keyword=' + $(this).parent().find('input[name="product_id"]').val(),
        success: function(response) {
          var data = JSON.parse(response);
          $cell.find('.bName').html(data.brand_name); // Problem is here
          $cell.find('.company_id').val(data.company_id); // and here
          console.log(data);
        },
      });
    });
    
    Login or Signup to reply.
  2. Unable to test using AJAX but perhaps this might help. Use the event of the click function to find the parentNode and from that use querySelector to target the particular elements in the table row.

    $(document).ready(function() {
      $('button[name="exchange"]').click(function(e) {
        let tr=e.target.parentNode;
        let span=tr.querySelector('span.bName');
        let pid=tr.querySelector('input[name="product_id"]');
        let cid=tr.querySelector('input[name="company_id[]"]');
        
        console.info( span.textContent, cid.value, pid.value)
    
        $.ajax({
          url: 'ajaxChangeBrand.php',
          type: 'POST',
          data: 'keyword=' + $(this).parent().find('input[name="product_id"]').val(),
          success: function(response) {
            var data = JSON.parse(response);
            span.textContent=data.brand_name;
            cid.value=data.company_id;
            pid.value=data.product_id;
          },
        });
        
        
        
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <tr>
        <td>
          <span class="bName">Womble</span>
          <input name="product_id" type="number" value="23">
          <input name="company_id[]" type="number" value="88">
          <button name="exchange" type="button">Click Me!</button>
        </td>
      </tr>
      <tr>
        <td>
          <span class="bName">Bagpuss</span>
          <input name="product_id" type="number" value="39">
          <input name="company_id[]" type="number" value="12">
          <button name="exchange" type="button">Click Me!</button>
        </td>
      </tr>
      <tr>
        <td>
          <span class="bName">Clanger</span>
          <input name="product_id" type="number" value="47">
          <input name="company_id[]" type="number" value="91">
          <button name="exchange" type="button">Click Me!</button>
        </td>
      </tr>
    </table>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search