读取所选变量,然后从表格发送回控制器

时间:2015-05-15 13:43:42

标签: javascript php mysql codeigniter

我有一个表通过codeigniter控制器调用数据库中的数据。我想要做的是从表中读取选定的值将其发送回控制器并使用这些值从DB检索新记录然后加载他们再次进入页面。

我的控制器:

<?php
  if (!defined('BASEPATH'))
    exit('No direct script access allowed');

     class Dashboard1 extends CI_Controller {

      public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
         $this->load->model('dash_match_model');
         $this->session->keep_flashdata('supplier_id');
        $this->load->helper('url');
          $this->load->database();
    }

    public function index() {
       $arr['page']='dash1';
       $arr['present_all_suppliers'] = $this->dash_match_model->dash_present_all_suppliers();

        $arr['supplierCompany'] = "";

            $this->load->view('clients/clDashboard',$arr);

    }

    public function select_supplier() {

        $supplierCompany = $this->input ->post('supplierCompany');
        $arr['present_all_suppliers'] = $this->dash_match_model->dash_present_all_suppliers();
        $arr['supplierCompany'] = $supplierCompany;

        $supplier_selected = $this->user_model->return_selected_supplier($supplierCompany);
            foreach($supplier_selected->result() as $row)
            {
                $this->session->set_flashdata('supplier_id', $row->supplier_id);


            }

        $this->load->view('unspscSegment',$arr);
    }
}

我的视图文件的表格的具体行:

<table id="suppliertable" class="table table-striped table-bordered bootstrap-datatable datatable">
    <thead>
        <tr>
            <th>Supplier</th>
            <th>Open Range</th>
            <th>Fill Content</th>
            <th>Total Match</th>
        </tr>
    </thead>   
    <tbody>

        <?php foreach ($present_all_suppliers as $v): ?>
            <tr>

                <td onclick="window.location = 'http://example.com'" class="center" style="color:#0c595b;"> <?php echo $v->supplierCompany; ?> </td>
                <td class="center"></td>
                <td class="center"></td>
                <td class="center"></td>
            </tr>
        <?php endforeach; ?>


    </tbody>
</table>

我如何使用javascript这样做,所以当我选择供应商(值)来获取所选值并将其发送到控制器?那么我应该向控制器修改什么才能将值发送到模型并收集它并在视图中重新加载它们?

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery.ajax()

在您的代码中实现:

脚本:

<script>
    $(".supplier_edit").click(function(){
        var supplier_company = $(this).find('input').val()
        $.ajax({
            type: 'POST',
            url: 'http://example.com/'+'Dashboard1/select_supplier',
            data: {supplierCompany: supplier_company},
            success: function(result) {
                $("#content").html(result)
            }
        })
        return false
    })
</script>

HTML:

<tbody>
    <?php foreach ($present_all_suppliers as $v): ?>
        <tr>
            <td class="supplier_edit" style="color:#0c595b;"><input type="hidden" value="<?php echo $v->supplierCompany; ?>" ></td>
            <td class="center"></td>
            <td class="center"></td>
            <td class="center"></td>
        </tr>
    <?php endforeach; ?>
</tbody>

<div id="content">content from the controller</div>