這是我第一次將ajax與搜索輸入框結合使用。我的AJAX工作正常,但我不知道如何從模型和控制器正確返回搜索數據。Codeigniter - 來自控制器/模型問題的Ajax返回數據
的控制器: 「你已經找到twiiter ID的」 工作正常,但是$ NUM不會通過
function search()
{
$this->form_validation->set_rules('twit', 'Search', 'required|trim|min_length[2]|max_length[25]');//check
if ($this->form_validation->run() == TRUE) {
$this->load->model('twit_model');
$query = $this->twit_model->entity();
if(!$query = $this->twit_model->entity())
{
echo "The name does not exist";
}else{
echo "<h3>You found $num Twitter ID's</h3>"; //this shows up in the view
echo "<li class=\"list1\">$twit_id - $name</li> "; //the "-" shows up in the view
}
}
}
模型來。
function entity()
{
$twit = $this->input->post('twit');
$this->db->select('id, name, state, party, twit_id, job');
$this->db->like('name', $twit);
$this->db->or_like('state', $twit);
$this->db->or_like('party', $twit);
$this->db->or_like('twit_id', $twit);
$this->db->or_like('job', $twit);
$query = $this->db->get('twit');
$num = $query->num_rows();
if($query->num_rows() > 0) {
$data - array(
$query,
$num
);
return $data;
}
}
的控制器和模型做工精細的基礎知識,我知道在這兩個我沒有從模型傳遞數據給控制器正確
jQuery的結束
$(function() {
$('#display').hide();
$('#name_error').hide();
$('#submit').click(function() { // could be #form .submit
var twit = $("#twitter_search").val();
if (twit == "") {
$("label#name_error").show();
$("input#twitter_search").focus();
return false;
}
var datastring = $('#form').serialize();
$.ajax({
url: "<?php echo site_url('twitter/search'); ?>",
type: "POST",
data: datastring,
success: function(msg) {
$('#display').html(msg).show(3000);
}
});
return false;
});
});
jquery工作的很好,警報/螢火蟲告訴我,我正在將數據傳遞給控制器。我只是不知道如何寫可變數據的傳遞到模型 - >控制器 - >查看
感謝您閱讀
謝謝icchanobot,這非常有幫助。我很感激 – Brad 2011-04-14 02:47:53