0

I used an array to print the data in several dynamically generated drop-down lists, but the result is very disappointing and does not bring the expected values from the database in the drop-down lists that I generate when needed, noting that the first drop-down list, which is outside the dynamic generation context, works well and brings the expected data, but the problem is Any new list generated dynamically, the values will only appear with the word "undefined" Any solution will be greatly appreciated...

here problem as GIF amage php code: `

<td class="td"><select class="form-control select_acount" name="account[]"required>
                            <option value="">--الحساب--</option>
                            <?php
                            $query = "select * from sah7atmain_acouts WHERE company like '".$multi."'";
                            $result = mysqli_query($con,$query);
                            $data = array();
                            if($result)
                            {
                              while($row = mysqli_fetch_assoc($result)){
                              $data[] = $row;
                            ?>
                            <?php echo '<option value="'.$row['acount_mounte'].'">'.$row['acount_name'].'</option>'?>;
                            <?php
                            }
                            } else{
                            }
                            ?>
                            </select>
                          </td>

jquery dynamic genaration code:

var data = '<?php echo $data; ?>';
      var product_dd = "";
          product_dd += '<select class="form-control select_acount" name="account[]"required>';
          product_dd += '<option value="">--الحساب--</option>';
      if(data.length > 0){
        for(let i = 0; i < data.length; i ++) {
          product_dd += `<option value="${data[i]['acount_mounte']}">${data[i]['acount_name']}</option>`;
        }
      }
         product_dd += "</select>";
      
      var i = 0;
      $("#add-btn").click(function() {
        ++i;
        
        $("#dynamicAddRemove").append('<tr><td class="td">'+product_dd+'</td> <td class="td"><input type="number" name="debtor[' + i + ']" id="fname"class="form-control debtor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="number" name="creditor[' + i + ']" id="james" class="form-control creditor arabicNumbers" onkeydown="return event.keyCode !== 69" required></td> <td class="td"><input type="text"  name="description[' + i + ']" class="form-control"  required></td> <td> <input type="text" name="mount[' + i + ']" class="form-control mount"   required> </td><button type="button" name="add" class="btn btn-danger remove-tr"><i class="fas fa-trash-alt"></i></button></td></tr>');
      });
      $(document).on('click', '.remove-tr', function() {
        $(this).parents('tr').remove();
      });

`

  • 1
    This will not work: `var data = '';` it just prints `var data = 'Array';`, you have to do it like this: `var data = ` to get an useable data array for javascript. https://stackoverflow.com/questions/4885737/pass-a-php-array-to-a-javascript-function – Foobar Dec 23 '22 at 12:42
  • still not working –  Dec 23 '22 at 13:02
  • 1
    Use the debug console in your browser mostly F12. May you have to fix other problems. – Foobar Dec 23 '22 at 13:03

0 Answers0