所以我尝试通过codeignighter框架设置我的自动完成框,我似乎无法让文本框填充数据库中的结果。
这是我的模特
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class nextCreateCampaign_model extends CI_Model {
public function __construct()
{
parent::__construct();
// Your own constructor code
}
function getUser($term)
{
$sql = $this->db->query('select campaign_name, remote_service_ID from ad_report_new.service where remote_service_ID like "'. mysql_real_escape_string($term) .'%" order by remote_service_ID');
return $sql ->result();
}
}
?>
这是我的Controller campaign.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Campaign extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/campaign
* - or -
* http://example.com/index.php/campaign/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/campaign/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
function __construct()
{
parent::__construct();
$this->load->model('nextCreateCampaign_model');
}
public function index(){
$this->load->helper('url');
$this->load->view('nextCreateCampaign');
}
public function getUserEmail()
{
if ( !isset($_GET['term']) )
exit;
$term = $_REQUEST['term'];
$data = array();
$rows = $this->nextCreateCampaign_model->getUser($term);
foreach( $rows as $row )
{
$data[] = array(
'label' => $row->remote_service_ID.', '. $row->email,
'value' => $row->remote_service_ID); // here i am taking name as value so it will display name in text field, you can change it as per your choice.
}
echo json_encode($data);
flush();
}
}
/* End of file campaign.php */
/* Location: ./application/controllers/campaign.php */
最后我的观点
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Create/Edit Pay For Performance Campaigns</title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#txtinput" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "<?php echo base_url();?>index.php/campaign/getUserEmail",{
term: extractLast( request.term )
},response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 1 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( "," );
return false;
}
});
});
</script>
</head>
<style>
#txtinput{width:400px;height: 30px;border-radius:8px;border:1px solid #999;}
</style>
<body>
<input type="text" id="txtinput" size="20" />
</body>