codeigniter - 使用输入数据在同一表单上显示数据库查询结果 [英] codeigniter - display database query result on same form using input data
问题描述
我已经拖网捕鱼了一段时间,现在试图找到一个冷的帮助我,但没有运气的解决方案。
我有一个简单的销售形式用户从下拉列表中选择产品。在选择值时,我想要输入框值被传递到数据库查询,并且查询结果(价格)显示在窗体上。如果可能,我希望结果填充一个输入框,以便销售人员可以根据需要调整。
我使用codeigniter,使得找到一个很好的例子很难。 >
控制器
function new_blank_order_lines $ b {
$ this-> load-> view('sales / new_blank_order_lines');
}
模型
function get_sku_price($ q){
$ this-> db-> select('ProductPrice');
$ this-> db-> where('ProductCode',$ q);
$ query = $ this-> db-> get('ProductList');
if($ query-> num_rows> 0){
foreach($ query-> result_array()as $ row){
$ row_set [] = htmlentities(stripslashes row ['ProductPrice'])); // build a array
}
$ this-> output-> set_content_type('application / json') - > set_output(json_encode($ row_set));
}
}
查看
< table>
< tr>< td>产品< / td>< td>价格< / td>< / tr>
< tr>
< td>< select name =product>
< option =sku1> product 1< / option>
< option =sku2>产品2< / option>
< option =sku3>产品3< / option>
< select>< / td>
< td>< input type =textid =pricename =price/>< / td>
< / tr>
< / table>
我已经加载了jquery库,1.9.1。
我有自动完成工作,但sytax是不一样的。
所以我想要的是,当我选择一个产品代码从产品
下拉列表,将值传递给模型,然后在输入框中显示查询结果(price) price
。
任何人都可以提供一些有关如何执行此操作的洞察,或者是一个很好的工作示例。
感谢百万,这个社群真棒!
Fabio
function new_blank_order_lines()
{
$ this-> load-> view /新命令');
}
视图:
< script>
$(#product)。change(function(){
//获取更改时的选择值
var value = $(#product)。val )
//将ajax请求发布到您的控制器
$ .post('<?= base_url(sales / get_sku_prices)?>',{data:value }; function(result){
//用返回值改变输入价格
$('#price')。value(result);
});
} );
< / script>
< table>
< tr>< td>产品< / td>< td>价格< / td>< / tr>
< tr>
< td>< select name =productid =product>
< option value =sku1>产品1< / option>
< option value =sku2>产品2< / option>
< option value =sku3>产品3< / option>
< / select>< / td>
< td>< input type =textid =pricename =price/>< / td>
< / tr>
< / table>
控制器要提取数据库数据:
function get_sku_prices(){
//检查if是否为ajax请求
if($ this-> input-> is_ajax_request()){
//检查在发布数据上是否存在变量数据
if($ this-> input-> post('data')){
$ this-> load-> model 'Sales_model');
//在你的模型中查询你应该在查询之前验证数据是否合法
$ price = $ this-> your_model-> get_sku_price($ this-> input-> post 'data',TRUE));
echo $ price;
}
}
}
型号:
function get_sku_price($ q){
$ this-> db-> select('ProductPrice');
$ this-> db-> where('ProductCode',$ q);
$ query = $ this-> db-> get('ProductList');
if($ query-> num_rows> 0){
foreach($ query-> result_array()as $ row){
$ row_set [] = htmlentities row ['ProductPrice'])); // build a array
}
$ this-> output-> set_content_type('application / json') - > set_output(json_encode($ row_set));
}
}
您的视图:
< table&
< tr>
< td>产品< / td>
< td>价格< / td>
< / tr>
< tr> ¥b $ b< td>
< select name =productid =product>
< option value =sku1>产品1< / option>
< option value =sku2>产品2< / option>
< option value =sku3>产品3< / option>
< / select>
< / td> ¥b $ b< td>
< input type =textid =pricename =price/>
< / td>
< / tr>
< / table>
javascript
< script>
$(#product)。change(function(){
//获取更改时的选择值
var value = $(#product)。val )
//将ajax请求发送到控制器
$ .post('<?= site_url(controller / function)?>',{data:value }; function(result){
//用返回值改变输入价格
$('#price')。value(result);
});
} );
< / script>
控制器:
public function your_funtion(){
//检查是否为ajax请求
if($ this-> input-> is_ajax_request()){
//检查在发布数据上是否存在变量数据
if($ this-> input-> post('data')){
$ this-> load_model('your_model')
//在你的模型中查询你应该在查询之前验证数据是否合法
$ price = $ this-> your_model-> get_price($ this-> input-> post ',TRUE));
echo $ price;
}
}
}
I have trawled the net for sometime now trying to find a solution which cold assist me, but have had no luck.
I have a simple sales form in which the user selects a product from a drop down list. on selecting a value, I want the input box value to get passed to a database query, and the query result (price) be displayed on the form. if possible I want the result to populate an input box so the salesman can adjust as needed.
I am using codeigniter which makes finding a good example quite difficult.
Controller
function new_blank_order_lines()
{
$this->load->view('sales/new_blank_order_lines');
}
Model
function get_sku_price($q){
$this->db->select('ProductPrice');
$this->db->where('ProductCode', $q);
$query = $this->db->get('ProductList');
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row['ProductPrice'])); //build an array
}
$this->output->set_content_type('application/json')->set_output(json_encode($row_set));
}
}
View
<table>
<tr><td>Product</td><td>Price</td></tr>
<tr>
<td><select name="product">
<option="sku1">product 1</option>
<option="sku2">product 2</option>
<option="sku3">product 3</option>
<select></td>
<td><input type="text" id="price" name="price" /></td>
</tr>
</table>
I have loaded the jquery library, 1.9.1.
I have got autocomplete working but the sytax is just not the same.
So what I am wanting, is that when I select a product code from the product
drop down list, the value is passed to the model, the query result(price) is then displayed in the input box price
.
Can anyone provide some insight on how to do this, or a good working example?
Thanks a million, this community is awesome!
Fabio
Controller:
function new_blank_order_lines()
{
$this->load->view('sales/new_order');
}
The view:
<script>
$("#product").change(function () {
//get the value of the select when it changes
var value = $("#product").val()
//make an ajax request posting it to your controller
$.post('<?=base_url("sales/get_sku_prices")?>', {data:value},function(result) {
//change the input price with the returned value
$('#price').value(result);
});
});
</script>
<table>
<tr><td>Product</td><td>Price</td></tr>
<tr>
<td><select name="product" id="product">
<option value="sku1">product 1</option>
<option value="sku2">product 2</option>
<option value="sku3">product 3</option>
</select></td>
<td><input type="text" id="price" name="price" /></td>
</tr>
</table>
Controller to fetch database data:
function get_sku_prices(){
//check if is an ajax request
if($this->input->is_ajax_request()){
//checks if the variable data exists on the posted data
if($this->input->post('data')){
$this->load->model('Sales_model');
//query in your model you should verify if the data passed is legit before querying
$price = $this->your_model->get_sku_price($this->input->post('data', TRUE));
echo $price;
}
}
}
Model:
function get_sku_price($q){
$this->db->select('ProductPrice');
$this->db->where('ProductCode', $q);
$query = $this->db->get('ProductList');
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row['ProductPrice'])); //build an array
}
$this->output->set_content_type('application/json')->set_output(json_encode($row_set));
}
}
Your View:
<table>
<tr>
<td>Product</td>
<td>Price</td>
</tr>
<tr>
<td>
<select name="product" id="product">
<option value="sku1">product 1</option>
<option value="sku2">product 2</option>
<option value="sku3">product 3</option>
</select>
</td>
<td>
<input type="text" id="price" name="price" />
</td>
</tr>
</table>
The javascript
<script>
$("#product").change(function () {
//get the value of the select when it changes
var value = $("#product").val()
//make an ajax request posting it to your controller
$.post('<?=site_url("controller/function")?>', {data:value},function(result) {
//change the input price with the returned value
$('#price').value(result);
});
});
</script>
The controller:
public function your_funtion(){
//check if is an ajax request
if($this->input->is_ajax_request()){
//checks if the variable data exists on the posted data
if($this->input->post('data')){
$this->load_model('your_model')
//query in your model you should verify if the data passed is legit before querying
$price = $this->your_model->get_price($this->input->post('data', TRUE));
echo $price;
}
}
}
这篇关于codeigniter - 使用输入数据在同一表单上显示数据库查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!