This is my first foray into using ajax with a search input box. My ajax works fine however I do not know how to properly return the search data from the model and controller.
The controller: The "You have found twiiter ID's" works fine, however the $num does not come through
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
}
}
}
The model.
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;
}
}
The basics of the controller and model work fine, I realize at the end of both I am not passing the data from the model to the controller correctly
The 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;
});
});
The jquery works just fine, alerts /firebug tell me I am passing the data to the controller. I just dont know how to write the passing of the variable data to the model->controller->view
Thanks for reading
It kinda just looks like you are using variables out of scope.
in your model you should change this:
$data - array(
$query,
$num
);
to something like this:
$data = array('query' => $query, 'count' => $num, 'twit_id'=>$twit);
then in your controller change this:
echo "<h3>You found $num Twitter ID's</h3>";
echo "<li class=\"list1\">$twit_id - $name</li> ";
into something like this:
echo "<h3>You found ".$query['count']." Twitter ID's</h3>";
echo "<li class=\"list1\">".$query['twit_id']." - ".$query['query']."</li> ";
$data - array(
$query,
$num
);
did you make a typo here? should it be $data = array? besides, the variable $num inside the search function is neither b defined nor returned.
精彩评论