-1

The loop works fine, it display what it supposed to do, but there is a mysql update button of which updates only the first row value displayed:

$(document).ready(function() {
  $("#update").click(function() {

    var fname = $("#fname").val();
    var lname = $("#lname").val();
    var recordId = $("#recordId").val();


    $.ajax({
      url: 'update.php',
      method: 'POST',
      async: true,

      data: {
        fname: fname,
        lname: lname,
        recordId: recordId
      },
      success: function(response) {
        alert(response);

      }

    });
  });
});
<?php
        $conn = new mysqli('localhost', 'root', '123456', 'lc');
        $sql = "SELECT * FROM lc where customer='souhail'";
        $result = $conn->query($sql);
       // while ( $row=mysqli_fetch_assoc($result)) {
            while($row = $result->fetch_array()) {
           echo  'LC ID :<input type="text" id="fname" value="'.$row['element_6'].'"><br>';         
           echo  'Status :<input type="text" id="lname" value="'.$row['element_101'].'"><br>';
            $recordId = $row['lc_id'];

           echo  '<input id="recordId" name="recordId" value="' . $recordId . '" >';

           ?>
  <button type="button" style="background-color:<?php
        if($row['element_101'] == '1'){
            echo '#0000FFF';

}elseif ($row['element_101'] == '2'){
            echo '#ffff00';

}elseif ($row['element_101'] == '3'){
            echo '#00FF00';

}elseif ($row['element_101'] == '4'){
            echo '#ffffff';

    }
        //echo $row['element_101'];
        ?>;color:#000000" id="update">Go Forward ></button>

  <br><br>
  <?php

        } ?>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
saleh zwy
  • 11
  • 4
  • 3
    Where's the loop? – Jeto Aug 12 '18 at 12:10
  • 3
    Almost impossible to answer given the lack of code but the ID attribute for an element MUST be unique - so the code above references IDs `fname`, `lname` and `recordID` which I'm guessing in the HTML are NOT unique. The same applies for the ID assigned to the button(s) – Professor Abronsius Aug 12 '18 at 12:11
  • complete code has been provided – saleh zwy Aug 12 '18 at 12:16
  • Possible duplicate of [Why is it a bad thing to have multiple HTML elements with the same id attribute?](https://stackoverflow.com/questions/7505350/why-is-it-a-bad-thing-to-have-multiple-html-elements-with-the-same-id-attribute) – Nico Haase Aug 12 '18 at 12:35

1 Answers1

0

You must have unique IDs but instead wrap each set in a container and access relative tags:

$(".update").on("click", function() {
  var $container = $(this).closest(".container");
  var fname = $container.find(".fname").val();
  var lname = $container.find(".lname").val();
  var recordId = $container.find(".recordId").val();

  $.ajax({
    url: 'update.php',
    method: 'POST',
    data: {
      fname: fname,
      lname: lname,
      recordId: recordId
    },
    success: function(response) {
      alert(response);
    },
    error: function(xhr, textStatus, error) {
      alert(xhr.statusText);
      console.log(textStatus);
      console.log(error);
    }
  });
});

Php

while($row = $result->fetch_array()) { 
  echo '<div class="container">
  echo 'LC ID  :<input type="text" class="fname" value="'.$row['element_6'].'"><br>'; 
  echo 'Status :<input type="text" class="lname" value="'.$row['element_101'].'"><br>'; $recordId = $row['lc_id']; 
  echo '<input class="recordId" name="recordId" value="' . $recordId . '">'; 
  echo '<button type="button" clsss="update">Go Forward ></button>';
  echo '/div>';
} ?>  
mplungjan
  • 169,008
  • 28
  • 173
  • 236