1678273200
Trong hướng dẫn CodeIgniter này, chúng ta sẽ tìm hiểu cách triển khai Trình đơn thả xuống động phụ thuộc vào trạng thái quốc gia trong Codeigniter 4 với Ajax và bootstrap 4. Trình đơn thả xuống động phụ thuộc vào trạng thái quốc gia và thành phố trong PHP Codeigniter 4 với Ajax
Tạo menu thả xuống phụ thuộc động trong ứng dụng Codeigniter 4 rất dễ dàng bằng cách sử dụng ajax; Trong hướng dẫn này, với sự trợ giúp của cơ sở dữ liệu Ajax, jQuery, Bootstrap và MySQL, một menu thả xuống Quốc gia, Thành phố phụ thuộc sẽ được tạo trong ứng dụng PHP Codeigniter 4.
Hãy thực hiện các bước sau để triển khai menu thả xuống động phụ thuộc vào thành phố của quốc gia, thành phố bằng cách sử dụng ajax trong các ứng dụng codeigniter 4:
Trong bước này, bạn sẽ tải xuống phiên bản Codeigniter 4 mới nhất, truy cập liên kết này https://codeigniter.com/download Tải xuống cấu hình Codeigniter 4 mới và giải nén cấu hình trong hệ thống cục bộ của bạn xampp/htdocs/. Và đổi tên thư mục tải xuống "demo"
Tiếp theo, bạn sẽ định cấu hình một số cài đặt cơ bản trong tệp app/config/app.php , vì vậy hãy truy cập application/config/config.php và mở tệp này trong trình soạn thảo văn bản.
Xác định url cơ sở như thế này
public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/demo/';
Trong bước này, bạn cần tạo một cơ sở dữ liệu có tên demo, vì vậy hãy mở PHPMyAdmin của bạn và tạo cơ sở dữ liệu có tên demo. Sau khi tạo thành công cơ sở dữ liệu, bạn có thể sử dụng truy vấn SQL bên dưới để tạo bảng trong cơ sở dữ liệu của mình.
CREATE DATABASE demo;
CREATE TABLE `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `states` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `cities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `countries` VALUES (1, 'USA', 1);
INSERT INTO `countries` VALUES (2, 'Canada', 1);
INSERT INTO `states` VALUES (1, 1, 'New York', 1);
INSERT INTO `states` VALUES (2, 1, 'Los Angeles', 1);
INSERT INTO `states` VALUES (3, 2, 'British Columbia', 1);
INSERT INTO `states` VALUES (4, 2, 'Torentu', 1);
INSERT INTO `cities` VALUES (1, 2, 'Los Angales', 1);
INSERT INTO `cities` VALUES (2, 1, 'New York', 1);
INSERT INTO `cities` VALUES (3, 4, 'Toranto', 1);
INSERT INTO `cities` VALUES (4, 3, 'Vancovour', 1);
Trong bước này, bạn cần kết nối dự án của chúng tôi với cơ sở dữ liệu. bạn cần truy cập app/Config/Database.php và mở tệp database.php trong trình soạn thảo văn bản. Sau khi mở tệp trong trình soạn thảo văn bản, bạn cần định cấu hình thông tin xác thực cơ sở dữ liệu trong tệp này như bên dưới.
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'demo',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
Trong bước này, hãy truy cập app/Models/ và tạo một mô hình ở đây có tên là Main_model.php. Sau đó, thêm đoạn mã sau vào tệp Main_model.php của bạn:
<?php
namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
class Main_model extends Model
{
public function __construct() {
parent::__construct();
//$this->load->database();
$db = \Config\Database::connect();
}
public function getCountries()
{
$this->db->from('countries');
$query=$this->db->get();
return $query->result();
}
public function getStates($postData)
{
$this->db->from('states');
$this->db->where('country_id',$postData['country_id']);
$query=$this->db->get();
return $query->result();
}
public function getCities($postData)
{
$this->db->from('cities');
$this->db->where('state_id',$postData['state_id']);
$query=$this->db->get();
return $query->result();
}
}
Trong bước này, hãy truy cập app/Controllers và tạo tên bộ điều khiển DropdownAjaxController.php . Trong bộ điều khiển này, bạn cần thêm các phương thức sau vào nó:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\Main_model;
class DropdownAjaxController extends Controller {
public function index() {
helper(['form', 'url']);
$this->Main_model = new Main_model();
$data['countries'] = $this->Main_model->getCountries();
return view('dropdown-view', $data);
}
public function getStates() {
$this->Main_model = new Main_model();
$postData = array(
'country_id' => $this->request->getPost('country_id'),
);
$data = $this->Main_model->getStates($postData);
echo json_encode($data);
}
public function getCities() {
$this->Main_model = new Main_model();
$postData = array(
'state_id' => $this->request->getPost('state_id'),
);
$data = $this->Main_model->getCities($postData);
echo json_encode($data);
}
}
Trong bước này, bạn cần tạo một tệp xem có tên dropdown-view.php và cập nhật mã sau vào tệp của mình:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="csrf-token" content="content">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Codeigniter 4 Dependent Country State City Dropdown using Ajax - tutsmake.com</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h2 class="text-success">Codeigniter 4 Dependent Country State City Dropdown using Ajax - tutsmake.com</h2>
</div>
<div class="card-body">
<form>
<div class="form-group">
<label for="country">Countries</label>
<select class="form-control" id="country_id">
<option value="">Select Country</option>
<?php foreach($countries as $c){?>
<option value="<?php echo $c->id;?>"><?php echo $c->name;?></option>"
<?php }?>
</select>
</div>
<div class="form-group">
<label for="state">States</label>
<select class="form-control" id="state_id">
</select>
</div>
<div class="form-group">
<label for="city">Cities</label>
<select class="form-control" id="city_id">
</select>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script type='text/javascript'>
// baseURL variable
var baseURL= "<?php echo base_url();?>";
$(document).ready(function(){
// City change
$('#country_id').change(function(){
var country_id = $(this).val();
// AJAX request
$.ajax({
url:'<?=base_url()?>/DropdownAjaxController/getStates',
method: 'post',
data: {country_id: country_id},
dataType: 'json',
success: function(response){
// Remove options
$('#state_id').find('option').not(':first').remove();
$('#city_id').find('option').not(':first').remove();
// Add options
$.each(response,function(index,data){
$('#state_id').append('<option value="'+data['id']+'">'+data['name']+'</option>');
});
}
});
});
// Department change
$('#state_id').change(function(){
var state_id = $(this).val();
// AJAX request
$.ajax({
url:'<?=base_url()?>/DropdownAjaxController/getCities',
method: 'post',
data: {state_id: state_id},
dataType: 'json',
success: function(response){
// Remove options
$('#city_id').find('option').not(':first').remove();
// Add options
$.each(response,function(index,data){
$('#city_id').append('<option value="'+data['id']+'">'+data['name']+'</option>');
});
}
});
});
});
</script>
</body>
</html>
Bây giờ, hãy truy cập trình duyệt và nhấp vào bên dưới URL.
http://localhost/demo/public/index.php/dropdown
Trình đơn thả xuống phụ thuộc động của Quốc gia và Thành phố trong Codeigniter 4 với Ajax; Trong ví dụ này, bạn sẽ tìm hiểu cách triển khai trình đơn thả xuống động trạng thái thành phố phụ thuộc trong Codeigniter 4 với Ajax và bootstrap 4.
Bài viết gốc lấy từ: https://www.tutsmake.com
1678273200
Trong hướng dẫn CodeIgniter này, chúng ta sẽ tìm hiểu cách triển khai Trình đơn thả xuống động phụ thuộc vào trạng thái quốc gia trong Codeigniter 4 với Ajax và bootstrap 4. Trình đơn thả xuống động phụ thuộc vào trạng thái quốc gia và thành phố trong PHP Codeigniter 4 với Ajax
Tạo menu thả xuống phụ thuộc động trong ứng dụng Codeigniter 4 rất dễ dàng bằng cách sử dụng ajax; Trong hướng dẫn này, với sự trợ giúp của cơ sở dữ liệu Ajax, jQuery, Bootstrap và MySQL, một menu thả xuống Quốc gia, Thành phố phụ thuộc sẽ được tạo trong ứng dụng PHP Codeigniter 4.
Hãy thực hiện các bước sau để triển khai menu thả xuống động phụ thuộc vào thành phố của quốc gia, thành phố bằng cách sử dụng ajax trong các ứng dụng codeigniter 4:
Trong bước này, bạn sẽ tải xuống phiên bản Codeigniter 4 mới nhất, truy cập liên kết này https://codeigniter.com/download Tải xuống cấu hình Codeigniter 4 mới và giải nén cấu hình trong hệ thống cục bộ của bạn xampp/htdocs/. Và đổi tên thư mục tải xuống "demo"
Tiếp theo, bạn sẽ định cấu hình một số cài đặt cơ bản trong tệp app/config/app.php , vì vậy hãy truy cập application/config/config.php và mở tệp này trong trình soạn thảo văn bản.
Xác định url cơ sở như thế này
public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/demo/';
Trong bước này, bạn cần tạo một cơ sở dữ liệu có tên demo, vì vậy hãy mở PHPMyAdmin của bạn và tạo cơ sở dữ liệu có tên demo. Sau khi tạo thành công cơ sở dữ liệu, bạn có thể sử dụng truy vấn SQL bên dưới để tạo bảng trong cơ sở dữ liệu của mình.
CREATE DATABASE demo;
CREATE TABLE `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `states` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `cities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `countries` VALUES (1, 'USA', 1);
INSERT INTO `countries` VALUES (2, 'Canada', 1);
INSERT INTO `states` VALUES (1, 1, 'New York', 1);
INSERT INTO `states` VALUES (2, 1, 'Los Angeles', 1);
INSERT INTO `states` VALUES (3, 2, 'British Columbia', 1);
INSERT INTO `states` VALUES (4, 2, 'Torentu', 1);
INSERT INTO `cities` VALUES (1, 2, 'Los Angales', 1);
INSERT INTO `cities` VALUES (2, 1, 'New York', 1);
INSERT INTO `cities` VALUES (3, 4, 'Toranto', 1);
INSERT INTO `cities` VALUES (4, 3, 'Vancovour', 1);
Trong bước này, bạn cần kết nối dự án của chúng tôi với cơ sở dữ liệu. bạn cần truy cập app/Config/Database.php và mở tệp database.php trong trình soạn thảo văn bản. Sau khi mở tệp trong trình soạn thảo văn bản, bạn cần định cấu hình thông tin xác thực cơ sở dữ liệu trong tệp này như bên dưới.
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'demo',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
Trong bước này, hãy truy cập app/Models/ và tạo một mô hình ở đây có tên là Main_model.php. Sau đó, thêm đoạn mã sau vào tệp Main_model.php của bạn:
<?php
namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
class Main_model extends Model
{
public function __construct() {
parent::__construct();
//$this->load->database();
$db = \Config\Database::connect();
}
public function getCountries()
{
$this->db->from('countries');
$query=$this->db->get();
return $query->result();
}
public function getStates($postData)
{
$this->db->from('states');
$this->db->where('country_id',$postData['country_id']);
$query=$this->db->get();
return $query->result();
}
public function getCities($postData)
{
$this->db->from('cities');
$this->db->where('state_id',$postData['state_id']);
$query=$this->db->get();
return $query->result();
}
}
Trong bước này, hãy truy cập app/Controllers và tạo tên bộ điều khiển DropdownAjaxController.php . Trong bộ điều khiển này, bạn cần thêm các phương thức sau vào nó:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\Main_model;
class DropdownAjaxController extends Controller {
public function index() {
helper(['form', 'url']);
$this->Main_model = new Main_model();
$data['countries'] = $this->Main_model->getCountries();
return view('dropdown-view', $data);
}
public function getStates() {
$this->Main_model = new Main_model();
$postData = array(
'country_id' => $this->request->getPost('country_id'),
);
$data = $this->Main_model->getStates($postData);
echo json_encode($data);
}
public function getCities() {
$this->Main_model = new Main_model();
$postData = array(
'state_id' => $this->request->getPost('state_id'),
);
$data = $this->Main_model->getCities($postData);
echo json_encode($data);
}
}
Trong bước này, bạn cần tạo một tệp xem có tên dropdown-view.php và cập nhật mã sau vào tệp của mình:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="csrf-token" content="content">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Codeigniter 4 Dependent Country State City Dropdown using Ajax - tutsmake.com</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h2 class="text-success">Codeigniter 4 Dependent Country State City Dropdown using Ajax - tutsmake.com</h2>
</div>
<div class="card-body">
<form>
<div class="form-group">
<label for="country">Countries</label>
<select class="form-control" id="country_id">
<option value="">Select Country</option>
<?php foreach($countries as $c){?>
<option value="<?php echo $c->id;?>"><?php echo $c->name;?></option>"
<?php }?>
</select>
</div>
<div class="form-group">
<label for="state">States</label>
<select class="form-control" id="state_id">
</select>
</div>
<div class="form-group">
<label for="city">Cities</label>
<select class="form-control" id="city_id">
</select>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script type='text/javascript'>
// baseURL variable
var baseURL= "<?php echo base_url();?>";
$(document).ready(function(){
// City change
$('#country_id').change(function(){
var country_id = $(this).val();
// AJAX request
$.ajax({
url:'<?=base_url()?>/DropdownAjaxController/getStates',
method: 'post',
data: {country_id: country_id},
dataType: 'json',
success: function(response){
// Remove options
$('#state_id').find('option').not(':first').remove();
$('#city_id').find('option').not(':first').remove();
// Add options
$.each(response,function(index,data){
$('#state_id').append('<option value="'+data['id']+'">'+data['name']+'</option>');
});
}
});
});
// Department change
$('#state_id').change(function(){
var state_id = $(this).val();
// AJAX request
$.ajax({
url:'<?=base_url()?>/DropdownAjaxController/getCities',
method: 'post',
data: {state_id: state_id},
dataType: 'json',
success: function(response){
// Remove options
$('#city_id').find('option').not(':first').remove();
// Add options
$.each(response,function(index,data){
$('#city_id').append('<option value="'+data['id']+'">'+data['name']+'</option>');
});
}
});
});
});
</script>
</body>
</html>
Bây giờ, hãy truy cập trình duyệt và nhấp vào bên dưới URL.
http://localhost/demo/public/index.php/dropdown
Trình đơn thả xuống phụ thuộc động của Quốc gia và Thành phố trong Codeigniter 4 với Ajax; Trong ví dụ này, bạn sẽ tìm hiểu cách triển khai trình đơn thả xuống động trạng thái thành phố phụ thuộc trong Codeigniter 4 với Ajax và bootstrap 4.
Bài viết gốc lấy từ: https://www.tutsmake.com
1596183830
Want to create unique, scalable web and app solutions?
At HourlyDeveloper.io, Expert CodeIgniter developer works particularly for you and your project.
You can Hire CodeIgniter Developer with an extensive variety of skill sets together with PHP, MySQL, PHP frameworks such as Laravel, CakePHP, and Zend, CMS, and e-commerce platforms such as WordPress, Drupal, Magento, WooCommerce, Shopify.
Consult with our experts: https://bit.ly/3hUdppScodeIgniter development services
#hire codeigniter developer #codeigniter developer #codeigniter development #codeigniter development company #codeigniter development services #codeigniter
1617355159
One of the fastest, lightest, reliable, and completely capable frameworks for a business web app development is the Codeigniter framework from PHP. CodeIgniter provides out-of-the-box libraries to perform operations like Sending Emails, Uploading Files, Managing Sessions, etc.
Want to build an excellent web application for your business?
Then WebClues Infotech is the right place where you could find highly skilled and expert CodeIgniter developers for hire. Share us your requirement, Conduct desired candidate interviews, and finally choose the one best suitably, it is that easy with WebClues Infotech.
So what are you waiting for? Get going on the path to business growth with WebClues Infotech
For more inquiry click here: https://www.webcluesinfotech.com/contact-us/
Hire CodeIgniter Developer: https://www.webcluesinfotech.com/hire-codeigniter-developer/
Email: sales@webcluesinfotech.com
#hire codeigniter developers #hire codeigniter development expert #hire codeigniter developers #hire codeigniter developers or programmers #hire an offshore codeigniter development team #codeigniter programmer
1618650571
SISGAIN brings you the best Codeigniter development services in New Jersey, USA. We provide you with applications that are safe and secure. Our team of Codeigniter web developers is experienced and skilled, they can give you a customized software application. With the help of our Codeigniter applications, we make the website world much easier. Codeigniter development helps to reduce the complexity. Our applications are simple, cost-effective, and elegant. Codeigniter applications development allows the developers to create broad solutions and provide them with great performance. We have PHP programming systems in our Codeigniter applications. For more information call us at +18444455767 or email us at hello@sisgain.com
#codeigniter development services #best codeigniter development company #codeigniter web developers #codeigniter development #codeigniter development company #software development
1611112146
Autocomplete textbox search from database in codeigniter 4 using jQuery Typeahead js. In this tutorial, you will learn how to implement an autocomplete search or textbox search with database using jquery typehead js example.
This tutorial will show you step by step how to implement autocomplete search from database in codeigniter 4 app using typeahead js.
https://www.tutsmake.com/codeigniter-4-autocomplete-textbox-from-database-using-typeahead-js/
#codeigniter 4 ajax autocomplete search #codeigniter 4 ajax autocomplete search from database #autocomplete textbox in jquery example using database in codeigniter #search data from database in codeigniter 4 using ajax #how to search and display data from database in codeigniter 4 using ajax #autocomplete in codeigniter 4 using typeahead js