gpt4 book ai didi

php - 自动完成多个文本框值

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:04 25 4
gpt4 key购买 nike

我是 codeigniter 的新手。我需要执行自动完成多个文本框值,我有 php 中的代码。我需要在 codeigniter 中写这个。我不知道如何在 codeigniter 中进行...有人可以帮我编写代码吗?这是我的 index.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<link rel="stylesheet" type="text/css" href="css/jquery-ui.min.css" />
</head>
<body>
<div class="container-fluid">
<div class="row main-content">
<div class="col-xs-12 col-sm-10 col-md-10 col-lg-10 col-sm-offset-2 col-md-offset-2 col-lg-offset-2">

<form id='students' method='post' name='students' action='index.php'>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th><input class='check_all' type='checkbox' onClick="select_all()"/></th>
<th>S. No</th>
<th>Country Name</th>
<th>Country Number</th>
<th>Country Phone code</th>
<th>Country code</th>
</tr>
<tr>
<td><input type='checkbox' class='case'/></td>
<td><span id='snum'>1.</span></td>
<td><input class="form-control" type='text' id='countryname_1' name='countryname[]'/></td>
<td><input class="form-control" type='text' id='country_no_1' name='country_no[]'/></td>
<td><input class="form-control" type='text' id='phone_code_1' name='phone_code[]'/></td>
<td><input class="form-control" type='text' id='country_code_1' name='country_code[]'/> </td>
</tr>
</table>
<button type="button" class='btn btn-danger delete'>- Delete</button>
<button type="button" class='btn btn-success addmore'>+ Add More</button>
</div>
</form>
</div>

</div>
</div><!-- /container -->

<div class="clearfix"></div>
<script src="js/auto.js"></script>
</body>
</html>

这是我的js文件auto.js

$(".delete").on('click', function() {
$('.case:checkbox:checked').parents("tr").remove();
$('.check_all').prop("checked", false);
check();
});
var i=$('table tr').length;

$(".addmore").on('click',function(){
count=$('table tr').length;

var data="<tr><td><input type='checkbox' class='case'/></td><td><span id='snum"+i+"'>"+count+".</span></td>";
data +="<td><input class='form-control' type='text' id='countryname_"+i+"' name='countryname[]'/></td> <td><input class='form-control' type='text' id='country_no_"+i+"' name='country_no[]'/></td><td><input class='form-control' type='text' id='phone_code_"+i+"' name='phone_code[]'/></td><td><input class='form-control' type='text' id='country_code_"+i+"' name='country_code[]'/></td></tr>";
$('table').append(data);
row = i ;
$('#countryname_'+i).autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'country_table',
row_num : row
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#country_no_'+id[1]).val(names[1]);
$('#phone_code_'+id[1]).val(names[2]);
$('#country_code_'+id[1]).val(names[3]);
}
});

$('#country_code_'+i).autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'country_table',
row_num : row
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[3],
value: code[3],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#country_no_'+id[1]).val(names[1]);
$('#phone_code_'+id[1]).val(names[2]);
$('#countryname_'+id[1]).val(names[0]);
}
});
$('#phone_code_'+i).autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'country_table',
row_num : row
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[2],
value: code[2],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#country_no_'+id[1]).val(names[1]);
$('#country_code_'+id[1]).val(names[3]);
$('#countryname_'+id[1]).val(names[0]);
}
});
$('#country_no_'+i).autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'country_table',
row_num : row
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[1],
value: code[1],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#country_code_'+id[1]).val(names[3]);
$('#phone_code_'+id[1]).val(names[2]);
$('#countryname_'+id[1]).val(names[0]);
}
});

i++;
});

function select_all() {
$('input[class=case]:checkbox').each(function(){
if($('input[class=check_all]:checkbox:checked').length == 0){
$(this).prop("checked", false);
} else {
$(this).prop("checked", true);
}
});
}

function check(){
obj=$('table tr').find('span');
$.each( obj, function( key, value ) {
id=value.id;
$('#'+id).html(key+1);
});
}

$('#countryname_1').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'country_table',
row_num : 1
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
$('#country_no_1').val(names[1]);
$('#phone_code_1').val(names[2]);
$('#country_code_1').val(names[3]);
}
});

$('#country_code_1').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'country_code',
row_num : 1
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[3],
value: code[3],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
$('#country_no_1').val(names[1]);
$('#phone_code_1').val(names[2]);
$('#countryname_1').val(names[0]);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});

$('#country_no_1').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'country_no',
row_num : 1
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[1],
value: code[1],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
$('#country_code_1 ').val(names[3]);
$('#phone_code_1').val(names[2]);
$('#countryname_1').val(names[0]);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});

$('#phone_code_1').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'phone_code',
row_num : 1
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[2],
value: code[2],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
$('#country_code_1 ').val(names[3]);
$('#country_no_1 ').val(names[1]);
$('#countryname_1').val(names[0]);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});

这是我的 ajax 文件 ajax.php

if($_POST['type'] == 'country_table'){
$row_num = $_POST['row_num'];
$name = $_POST['name_startsWith'];
$query = "SELECT name, numcode, phonecode, iso3 FROM country where UPPER(name) LIKE '".strtoupper($name)."%'";
$result = mysqli_query($con, $query);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['name'].'|'.$row['numcode'].'|'.$row['phonecode'].'|'.$row['iso3'].'|'.$row_num;
array_push($data, $name);
}
echo json_encode($data);
}

最佳答案

首先定义一个基本 Controller ,例如将调用 View 的索引。如下:

class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}
function index(){
$data['title']='View_Country';
$this->load->view('view_home',$data);
}
}

然后您需要定义一个 View (View_Country),它将包含 html 和 ajax 代码。

现在在 ajax 代码中,您必须修改 php 链接以调用 Controller 来获取数据。这可以按如下方式完成

var controller = 'country';
var base_url = '<?php echo site_url();?>';
url= base_url + controller + '/get_country

在 Controller 中添加一个与上面同名的函数,即“get_country”。所以现在你的基本 Controller 将如下所示:

class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}

function index(){
$data['title']='View_Country';
$this->load->view('view_home',$data);
}

function get_country(){
$this->load->model('get_country');
$data = array();
$data['list'] = $this->get_country->get_country_detail();
$this->load->view('view_country', $data);

}
}

使用此 Controller 调用一个模型,该模型将从数据库中获取实际数据。 您的 ajax.php 代码将在此模型中定义。如下所示

class get_country extends CI_Model {
function __construct(){
parent::__construct();
}
public function get_country_detail(){
$search = $this->input->get('term');
if($search == ''){
return array();
}
$query=$this->db->query("SELECT name, numcode, phonecode, iso3 FROM country where UPPER(name) LIKE '".strtoupper($name)."%'");
if($query->num_rows() > 0)
return $query->result();
else return NULL;
}
}

填充数据后,您可以调用 View 来显示结果。

<?php if(isset($list) && count($list)) : 
foreach($list as $row) :
$name = $row['name'].'|'.$row['numcode'].'|'.$row['phonecode'].'|'.$row['iso3'].'|'.$row_num;
array_push($data, $name);
endforeach;
echo json_encode($data);
endif; ?>

我刚刚向您概述了如何进行。您必须正确编码并按照您想要的方式完成它

关于php - 自动完成多个文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32562154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com