私はしばらくの間ネットをトロールして、私を冷静に助ける解決策を見つけようとしましたが、運がありませんでした。
ユーザーがドロップダウンリストから製品を選択する簡単な販売フォームがあります。値を選択する際に、入力ボックスの値をデータベースクエリに渡して、クエリ結果(価格)をフォームに表示したいと思います。可能であれば、セールスマンが必要に応じて調整できるように、結果を入力ボックスに入力する必要があります。
私はcodeigniterを使用しているため、良い例を見つけるのは非常に困難です。
コントローラ
function new_blank_order_lines()
{
$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 an array
}
$this->output->set_content_type('application/json')->set_output(json_encode($row_set));
}
}
意見
<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>
jqueryライブラリ1.9.1をロードしました。
オートコンプリートが機能していますが、構文は同じではありません。
したがって、私が望んでいるのは、product
ドロップダウンリストから製品コードを選択すると、値がモデルに渡され、クエリ結果(価格)が入力ボックスに表示されることprice
です。
誰かがこれを行う方法についての洞察、または良い実用的な例を提供できますか?
百万ありがとう、このコミュニティは素晴らしいです!
ファビオ
コントローラ:
function new_blank_order_lines()
{
$this->load->view('sales/new_order');
}
景色:
<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>
データベースデータをフェッチするコントローラー:
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;
}
}
}
モデル:
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));
}
}