2

I am fetching acno from table when i select a party name in option.I have so far tired i get the acno from the table but it is not place in the option box.

My controller code:

 public function get_states2()
    {
        $name = $this->input->post('name');
$result = $this->db->query("SELECT TAcNo FROM tipup_payment LEFT OUTER JOIN parmaster on parmaster.pcode =  tipup_payment.TName WHERE PName='$name' ")->result_array();
echo json_encode($result);
}

My View page code:

<div class="col-md-6">
<div class="form-group form-group-xs">
<div class="col-lg-9">
    Party Name:
<select class="form-control countries"  name="City">

<option></option>
<?php foreach ($PName as $row ): ?> 
<option value="<?php echo trim($row['PName']); ?>"><?php echo trim($row['PName']); ?></option><?php endforeach ?>
</select>
</div>
</div>
<div class="form-group form-group-xs">

<div class="col-lg-9">
    AcNo:
<select multiple="multiple" style="height: 85px;"  id="Name" class="form-control states">   
<option value=""></option>
</select> 
<?php echo form_error('Area', '<div class="text-danger">', '</div>'); ?>
</div>
</div>
<div id="item">
<input type="checkbox" name="item">With Details</center></div>
</div>
</div>

My Script Code:

    <script type="text/javascript">

    $(document).ready(function(){

$('.countries').change(function(){
    var name = $('.countries').val();
$.ajax({
            type: "POST",
            url: "<?php echo base_url();?>Tieup/get_states2",
            data:{name:name}, 
            datatype: 'json',

            success: function (data) {
                    /*get response as json */
                    alert(data);
                    var result = jQuery.parseJSON(data);
                var no = result.TAcNo;

                    $("#Name").val(no);

                    /*ends */

                }
            });
        });
    });

</script>

This is my view page when i select a party name it should display the acno in acno option box( it down the party name). enter image description here

4
  • what is the error ? Commented Oct 9, 2019 at 6:40
  • the value is not set in the acno box but i get the value from the table Commented Oct 9, 2019 at 6:43
  • can u give the response u get Commented Oct 9, 2019 at 6:50
  • i get value in alert box [{"TAcNo":"341"}] but it is not set in the acno box Commented Oct 9, 2019 at 6:51

2 Answers 2

1

give a class or id to ur dropdown ur html

<select class="product">

</select>

ur jquery code loop through all ur value and set it in ur option value one by one and at the end inject all ur html to ur select option using .html()

var value =  [{"TAcNo":"341"}]
var options =  '<option value="">Select</option>';


$(value).each((index, item) => { //loop through your elements
  console.log(item)
  options += '<option value="'+item.TAcNo+'">'+item.TAcNo+'</option>';
});

$('.product').html(options);

Hope it helps

Sign up to request clarification or add additional context in comments.

9 Comments

it is not set value in the option
it not shows an error but in alert box shows an acno
did u set the class/ id in ur select ? and where did u set this code inside success right ? and what did u get when u console.log(item) ?
the number is not refresh it again
when i select another name the first acno is set and it is not change to another no
|
1

Solution

you need to trigger change like this to update select value

$("#Name").val(no).change();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.