0

私はしばらくの間ネットをトロールして、私を冷静に助ける解決策を見つけようとしましたが、運がありませんでした。

ユーザーがドロップダウンリストから製品を選択する簡単な販売フォームがあります。値を選択する際に、入力ボックスの値をデータベースクエリに渡して、クエリ結果(価格)をフォームに表示したいと思います。可能であれば、セールスマンが必要に応じて調整できるように、結果を入力ボックスに入力する必要があります。

私は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));

    }
  }
4

2 に答える 2

1

あなたの見解:

<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>

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>

コントローラー:

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;
        }
    }
}
于 2013-03-19T20:47:10.123 に答える
0

jqueryのajaxを使用し、イベントを投稿または取得して変更します。ここで投稿を使用します

例..

 $('select[name="product"]').change(function(){
      var val=$(this).val();
      $.post('path/to/controller',{data:val},function(result){
          $('#price').val(result.price);
      }, "json");
 });

コントローラー機能

$product=$this->input->post('data');  //this will give you the selected value of select
//make query to db in model..get price and
$price = ..//price that you got from db
echo json_encode(array('price'=> $price));
于 2013-03-19T20:32:10.860 に答える