简体   繁体   中英

Getting issue while retrieving data from database into html dropdown using ajax and php?

I am retrieving the data from database as options into my dropdown and I am having an add button which will add the dropdowns here I am getting options from database so there are two options so every dropdown which I add should have two options here the problem is

  1. for the first time first dropdown is having two dropdowns and when I add another one and revisit the first dropdown is showing is four options that is the two options are being doubled like that when I add 3rd dropdown it is showing every option 3 times ie 9 options but I want to show everytime 2 options only the code is here
  2. I had kept disabled for add fa-fa-icon but it is not working ie if I select any option only the fa-fa-icon should enable I wrote the code for this but not working

 //Select option function $(document).on("change", ".drop", function() { //Enable button on selection $('#seedoc').prop('disabled', false) }) //response var res = { "users": ["<p style='margin:0px;display:none;'data-id='755'>amilham</p>", "<p style='margin:0px;display:none;'data-id='706'>a_sarabi</p>" ] } function getEmails() { res.users.forEach(function(option) { $('.drop').append('<option data-id=' + $(option).attr('data-id') + '>' + option + '</option>'); }); } function addOne() { $('#container').append("<div class='form-group' style='display:flex'><select class='drop form-control' name='option' id='option'> <option value='' disabled selected>Select your option</option> </select>"); getEmails(); } getEmails();
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type='text/css' rel='stylesheet' /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container" class=""></div> <i class="fa fa-plus-square fa-2x" aria-hidden="true" id="seedoc" style="float: right; margin-right: 10px;" onclick="addOne();" disabled></i> <div class='form-group' style='display:flex'> <select class='drop form-control' name='option' id='option'> <option value='' disabled selected>Select your option</option> </select> <button class="btn btn-primary shrBtn" style="float:left;" onclick="send()">Send</button> </div>

Please help me anyone thanks in advance!

You are appending the <option> tags every time you load new items from database. Simply clean dropdowns first, or replace the ones you already have with new ones:

function getEmails() {
  $('.drop').html('');
  res.users.forEach(function(option) {
    $('.drop').append('<option data-id=' + $(option).attr('data-id') + '>' + option + '</option>');
  });
}

OR

function getEmails() {
  let options = '';
  res.users.forEach(function(option) {
    options += '<option data-id=' + $(option).attr('data-id') + '>' + option + '</option>';
  });
  $('.drop').html(options);
}

Im not sure if icon can be disabled. If you dont want to wrap it with button, simply add condition in the event listener. First, instead of "disabled" set data-enabled="0" and removing disabled property and the "onclick=addOne()" call:

<i class="fa fa-plus-square fa-2x" aria-hidden="true" id="seedoc" style="float: right; margin-right: 10px;" data-enabled="0"></i>

Than, register action for icon click and add condition in it:

$(document).on("change", ".drop", function() {

  $('#seedoc').data().enabled = 1; //set data-enabled to true instead of disabled = false

});

$(document).on("click", "#seedoc", function(e) {

  if ($(this).data().enabled) addOne();

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM