PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 1, 2022

[FIXED] Data not populating in select tag with AJAX request

 January 01, 2022     ajax, codeigniter, jquery, php     No comments   

Issue

Currently I have a select tag that I want to fill in via a AJAX call every time the user clicks on that particular select tag. To do this I'm doing the following:

View class:

<label class="control-labels ">Property</label>
     <select name="property" class="form-control select2 selectsearch" <?php echo (isset($read) ? $read:''); ?> required>
     </select>

Ajax request:

$(document).ready(function(){  

    $("#property").click(function(){

    var post_url = 'listings/getonClick'
    $.ajax({
        type: "POST",
        url: post_url,
        data : { "hid" : $(this).val() },
        success: function(response){
            $.each(response, function(value, key) {
                $("#property").append("<option id="+value.id+">"+value.name+"</option>");
            })
        }
    });
});

Controller Class:

function getonClick(){

    $modelList = $this->listings_model->getProperties();

    echo(json_encode($modelList));
}

Model Class:

 function getProperties(){
    $this->db->select("id,name");
    $this->db->from("crm_project_properties");
    $query = $this->db->get();
    return $query->result_array() ;
 }

But after doing this, I cannot get any data in my select tag before or even after clicking on it


Solution

Can ypu please try with .on("click")? Its worked for me in past

$(document).ready(function () {
     $("#property").on("click", '*:not(a)', function() {
       var post_url = 'listings/getonClick'
         $.ajax({
           type: "POST",
           url: post_url,
           data : { "hid" : $(this).val() },
           success: function(response){
                $.each(response, function(value, key) {
                   $("#property").append("<option id="+value.id+">"+value.name+"</option>");
           })
         }
      });
});


Answered By - Madhuri Patel
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing