php - How to retrieve data from database using AJAX based on a select tag? -




let's imagine have 2 select tags

<select id='combobox_1'>    <option value='1'>description of fist record</option>    <option value='2'>description of second record</option> </select>  <select id='combobox_2'></select> 

and 2 tables on database

table_1

combox_1_db_id pk

combox_1_db_description

table_2

combox_2_db_id pk

combox_1_db_id fk

combox_2_db_description

now want send ajax request php value of option selected in combobox_1 fill records database combobox_2 based on combobox_1_id.

js

$('#combobox_1').on('change', function() {   var option_value = $('#combobox_1').find('option:selected').val();     $.ajax({     url: '/get_records',      method: 'get',      datatype: 'json',     success: function(data) {            $(data).each(function() {             var option = $('<option />');              option.attr('value',               this.combox_2_id).text(this.combox_2_description);               $('#combobox_2').append(option);         });     },     error: function() {         console.log('error on loading records');     } }); }); 

php

 $this->router->get_request('/get_records', function() {         $records= $this->soap_server->getrecords(array(             'combox1id' => $_get['combox_1_id']         ));         echo json_encode($records->return);  }); 

but don't find way send id php can records based on id , load them in combobox_2, appreciated.

you can send option_value in ajax, include data this...

$('#combobox_1').on('change', function() {  // id of selected option var id = $(this).children(":selected").attr("id");  $.ajax({     url: '/get_records',      method: 'get',      datatype: 'json',      data: {combox_1_id: id},  // send id in request     success: function(data) {            $(data).each(function() {             var option = $('<option />');              option.attr('value',               this.combox_2_id).text(this.combox_2_description);               $('#combobox_2').append(option);         });     },     error: function() {         console.log('error on loading records');     } }); }); 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -