У меня есть таблица в этой таблице имеет 3 поле (категория1, категория2, категория3). Теперь я хочу заполнить выпадающий список, если выпадает один из выбранных, выпадающие значения 2 будут Genarat из базы данных. Может ли кто -нибудь помочь мне. < /P>
Мой контроллер < /p>
$this->load->model('quiz_mode');
$arrStates = $this->quiz_mode->get_unique_catg1();
foreach ($arrStates as $states)
{
$arrFinal[$category->category1 ] = $category->category1 ;
}
$data['category2'] = $arrFinal;
function ajax_call()
{
//Checking so that people cannot go to the page directly.
if (isset($_POST) && isset($_POST['category1']))
{
$catogory = $_POST['category1'];
$arrcategory = $this->sampl_mode->get_cities_from_catgo1($catogory);
foreach ($arrcategory as $category)
{
$arrFinal[$category->category2] = $category->category2;
}
//Using the form_dropdown helper function to get the new dropdown.
print form_dropdown('category2',$arrFinal);
}
else
{
redirect('logged_in_user'); //Else redire to the site home page.
}
}
< /code>
Моя модель < /p>
function get_unique_catg1()
{
$query = $this->db->query("SELECT DISTINCT category1 FROM sampl_table");
if ($query->num_rows > 0) {
return $query->result();
}
}
function get_cities_from_catgo1($category) {
$query = $this->db->query("SELECT category2 FROM sampl_table WHERE category2 = '{$category}'");
if ($query->num_rows > 0) {
return $query->result();
}
}
< /code>
my view < /p>
untitled
$(document).ready(function () {
$('#catgo1-dropdown').change(function () {
var selCat1 = $('#catgo1-dropdown').val();
console.log(selCat1);
$.ajax({
url: "index.php/sample/ajax_call", //The url where the server req would we made.
async: false,
type: "POST", //The type which you want to use: GET/POST
data: "category1="+selCat1, //The variables which are going.
dataType: "html", //Return data type (what we expect).
//This is the function which will be called if ajax call is successful.
success: function(data) {
//data is the html of the page where the request is made.
$('#cotegory').html(data);
}
})
});
});
Подробнее здесь: https://stackoverflow.com/questions/113 ... odeigniter