Php 基于类别下拉式AJAX的数据库标题显示
我有一张桌子,看起来像:Php 基于类别下拉式AJAX的数据库标题显示,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,我有一张桌子,看起来像: id | page_name | category -------------------------- 1 | Tornado | Air 2 | Car | Vehicles 下拉列表,例如HTML: <select id="dropdown_categories"> <option value="Air">Air</option> <option value="Vehicles"
id | page_name | category
--------------------------
1 | Tornado | Air
2 | Car | Vehicles
下拉列表,例如HTML:
<select id="dropdown_categories">
<option value="Air">Air</option>
<option value="Vehicles">Vehicles</option>
</select>
型号:
function get_page_by_category ( $category ) {
$this->db->select('*')->from('page')->where('page.category =', $category);
$result = $this->db->get();
return $result;
}
阿贾克斯:
不确定如何继续,最好的方法是什么?您应该使用ajax。像这样试试
$(document).ready(function() {
$("#dropdown_categories").change(function(){
var val = $('#dropdown_categories option:selected').val();
$.ajax({
url: "path to controller function/value of select box present in var val",
type: "POST",
dataType: "HTML",
async: false,
success: function(data) {
alert(data)
}
});
});
});
在controller中,您可以回显值,这将使ajax在数据变量中获得成功,我在那里提醒过您,您可以使用jQuery,因此您可以使用jQuery ajax。这很容易。您可以使用:
var val = $('#dropdown_categories option:selected').val();
$.ajax({
url: "url",
type: "POST",
dataType: "application/json,"
data: {value: val, callback: php_function_to_call, args: [array_of_arguments_of_php_function]}
success: function(data){
document.title = data.title;
}
})
现在,php:
function myFunction($arguments_if_you_have){
//get the databse result here;
//notice: ajax only returns data, when there is something sent to the output stream, so we must use echo
echo json_encode($result);
}
if(isset($_POST['callback'])){
if(isset($_POST['args'])){
call_user_func_array($_POST['callback'], $_POST['args']);
}
call_user_func_array($_POST['callback'], array());
}
我认为控制器或模型存在一些问题。。如果我转到url get_page_by_category?category=Air,我有错误号:1064您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解第3行附近要使用的正确语法,从page WHERE page.category=Filename:/var/www/html/models/page_model.php行号:97是否使用get??或者它是一个函数参数??我已经在那里给出了函数参数,所以它应该是按类别/空气获取的
var val = $('#dropdown_categories option:selected').val();
$.ajax({
url: "url",
type: "POST",
dataType: "application/json,"
data: {value: val, callback: php_function_to_call, args: [array_of_arguments_of_php_function]}
success: function(data){
document.title = data.title;
}
})
function myFunction($arguments_if_you_have){
//get the databse result here;
//notice: ajax only returns data, when there is something sent to the output stream, so we must use echo
echo json_encode($result);
}
if(isset($_POST['callback'])){
if(isset($_POST['args'])){
call_user_func_array($_POST['callback'], $_POST['args']);
}
call_user_func_array($_POST['callback'], array());
}