依赖下拉菜单使用Codeigniter,没有Jquery和Ajax


Dependend DropDown using Codeigniter, No Jquery nor Ajax

我想使子扇区依赖于扇区,我不使用jquery或ajax

这是我的控制器

    <?php
        /*
        */
        if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        class Salesforce extends CI_Controller
        {
            public function __construct()
            {
                parent::__construct();
                $this->load->library('session');
                $this->load->helper('form');
                $this->load->helper('url');
                $this->load->database();
                $this->load->library('form_validation');
                //load the employee model
                $this->load->model('sf_model');
            }
            //index function
            function index()
            {
                //...

                $data['sector'] = $this->sf_model->get_sector();
                $data['subsector'] = $this->sf_model->get_subsector();
                $data['type'] = $this->sf_model->get_type();
                //set validation rules
                $this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
                $this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
                $this->form_validation->set_rules('a_type', 'Type', 'callback_combo_check');
                $this->form_validation->set_rules('a_sector', 'Sector', 'callback_combo_check');
                $this->form_validation->set_rules('a_subsector', 'Sub Sector', 'callback_combo_check');

                if ($this->form_validation->run() == FALSE)
                {
                    //fail validation
                    //echo 'Validation failed';
                    //print_r($_REQUEST);
                    $this->load->view('sf_view', $data);
                    //
                }
                else
                {
                    //pass validation
                    $data = array(
                        'a_name' => $this->input->post('a_name'),
                        'a_website' => $this->input->post('a_web'),
                        't_id' => $this->input->post('a_type'),
                        's_id' => $this->input->post('a_sector'),
                        'ss_id' => $this->input->post('a_subsector'),
                        'a_billingStreet' => $this->input->post('a_billingStreet'),
                        'a_billingState' => $this->input->post('a_billingState'),
                        'a_billingZip' => $this->input->post('a_billingZip'),
                        'a_billingCountry' => $this->input->post('a_billingCountry'),
                        'a_billingCity' => $this->input->post('a_billingCity'),
                        'a_phone' => $this->input->post('a_phone'),
                        'a_mobile' => $this->input->post('a_mobile'),
                        'a_fax' => $this->input->post('a_fax'),

                    );
                    //print_r($data);
                    //exit;
                    //insert the form data into database
                    $this->db->insert('account_info', $data);
                    //display success message
                    $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">New Account Created</div>');
                    redirect('salesforce/index');
                }
            }


            //custom validation function for dropdown input
            function combo_check($str)
            {
                if ($str == '-SELECT-')
                {
                    $this->form_validation->set_message('combo_check', 'Valid %s Name is required');
                    return FALSE;
                }
                else
                {
                    return TRUE;
                }
            }
                //custom validation function to accept only alpha and space input
            function alpha_only_space($str)
            {
                if (!preg_match("/^([-a-z ])+$/i", $str))
                {
                    $this->form_validation->set_message('alpha_only_space', 'The %s field must contain only alphabets or spaces');
                    return FALSE;
                }
                else
                {
                    return TRUE;
                }
            }

        }

这是我的模型

<?php
        class Sf_model extends CI_Model
        {
            function __construct()
            {
                parent::__construct();
            }

            function get_sector()
            {
                $results = $this->db->select('s_id, s_name')->from('account_sector')->get()->result();
                $s_id = array('-SELECT-');
                $s_name = array('-SELECT-');
                for ($i = 0; $i < count($results); $i++)
                {
                    array_push($s_id, $results[$i]->s_id);
                    array_push($s_name, $results[$i]->s_name);
                }
                return $sector_result = array_combine($s_id, $s_name);
            }
            function get_subsector()
            {
                $results = $this->db->select('ss_id, ss_name')->from('account_subsector')->get()->result();
                $ss_id = array('-SELECT-');
                $ss_name = array('-SELECT-');
                for ($i = 0; $i < count($results); $i++)
                {
                    array_push($ss_id, $results[$i]->ss_id);
                    array_push($ss_name, $results[$i]->ss_name);
                }
                return $subsector_result = array_combine($ss_id, $ss_name);
            }
            function get_type()
            {
                $results = $this->db->select('t_id, t_name')->from('account_type')->get()->result();
                $t_id = array('-SELECT-');
                $t_name = array('-SELECT-');
                for ($i = 0; $i < count($results); $i++)
                {
                    array_push($t_id, $results[$i]->t_id);
                    array_push($t_name, $results[$i]->t_name);
                }
                return $type_result = array_combine($t_id, $t_name);
            }
        }

> 

This is my View

        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>SalesForce | Edit / New Account Details</title>
            <!--link the bootstrap css file-->
            <link href="<?php echo base_url("bt/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
            <!-- link jquery ui css-->
            <link href="<?php echo base_url('bt/css/jquery-ui.min.css'); ?>" rel="stylesheet" type="text/css" />
            <!--include jquery library-->
            <script src="<?php echo base_url('bt/js/jquery-1.11.3.js'); ?>"></script>
            <!--load jquery ui js file-->
            <script src="<?php echo base_url('bt/js/jquery-ui.min.js'); ?>"></script>
            <style type="text/css">
                .colbox {
                    margin-left: 0px;
                    margin-right: 0px;
                }
            </style>
        </head>
        <body>
        <div class="container">
            <div class="row">
                <div class="col-sm-offset-3 col-lg-6 col-sm-6 well">
                    <legend>Account Information</legend>
                    <?php
                    $attributes = array("class" => "form-horizontal", "id" => "accounts", "name" => "accounts");
                    echo form_open("salesforce", $attributes);?>
                    <fieldset>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_name" class="control-label">Account Name</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control"  value="<?php echo set_value('a_name'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_name'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_web" class="control-label">Website</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_web" name="a_web" placeholder="Enter Website" type="text" class="form-control"  value="<?php echo set_value('a_web'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_web'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_type" class="control-label">Type</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <?php
                                    $attributes = 'class = "form-control" id = "a_type"';
                                    echo form_dropdown('a_type',$type,set_value('a_type'),$attributes);?>
                                    <span class="text-danger"><?php echo form_error('a_type'); ?></span>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_sector" class="control-label">Sector</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <?php
                                    $attributes = 'class = "form-control" id = "a_sector"';
                                    echo form_dropdown('a_sector',$sector,set_value('a_sector'),$attributes);?>
                                    <span class="text-danger"><?php echo form_error('a_sector'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_subsector" class="control-label">Sub Sector</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <?php
                                    $attributes = 'class = "form-control" id = "a_subsector"';
                                    echo form_dropdown('a_subsector',$subsector, set_value('a_subsector'), $attributes);?>
                                    <span class="text-danger"><?php echo form_error('a_subsector'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_billingStreet" class="control-label">Billing Street</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_billingStreet" name="a_billingStreet" placeholder="Enter Billing Street" type="text" class="form-control"  value="<?php echo set_value('a_billingStreet'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_billingStreet'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_billingState" class="control-label">B-State/Province</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_billingState" name="a_billingState" placeholder="Enter State/Province" type="text" class="form-control"  value="<?php echo set_value('a_billingState'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_billingState'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_billingZip" class="control-label">B-Zip/Postal</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_billingZip" name="a_billingZip" placeholder="Enter Zip/Postal" type="text" class="form-control"  value="<?php echo set_value('a_billingZip'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_billingZip'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_billingCountry" class="control-label">Billing Country</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_billingCountry" name="a_billingCountry" placeholder="Enter Billing Country" type="text" class="form-control"  value="<?php echo set_value('a_billingCountry'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_billingCountry'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_billingCity" class="control-label">Billing City</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_billingCity" name="a_billingCity" placeholder="Enter Billing City" type="text" class="form-control"  value="<?php echo set_value('a_billingCity'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_billingCity'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_phone" class="control-label">Phone</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_phone" name="a_phone" placeholder="Enter Phone Number" type="text" class="form-control"  value="<?php echo set_value('a_phone'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_phone'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_mobile" class="control-label">Mobile Number</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_mobile" name="a_mobile" placeholder="Enter Mobile Number" type="text" class="form-control"  value="<?php echo set_value('a_mobile'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_mobile'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row colbox">
                                <div class="col-lg-4 col-sm-4">
                                    <label for="a_fax" class="control-label">Fax Number</label>
                                </div>
                                <div class="col-lg-8 col-sm-8">
                                    <input id="a_fax" name="a_fax" placeholder="Enter Fax Number" type="text" class="form-control"  value="<?php echo set_value('a_fax'); ?>" />
                                    <span class="text-danger"><?php echo form_error('a_fax'); ?></span>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-offset-4 col-lg-8 col-sm-8 text-left">
                                <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Insert" />
                                <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel" />
                            </div>
                        </div>
                    </fieldset>
                    <?php echo form_close(); ?>
                    <?php echo $this->session->flashdata('msg'); ?>
                </div>
            </div>
        </div>
        </body>
        </html>

控制器

   $data['sector'] = $this->sf_model->get_sector();
   if(isset($data['sector'])&&$data['sector']!="")
   $data['subsector'] = $this->sf_model->get_subsector($data['sector']);

模型,在模型中传递扇区作为get_subsector函数的参数,然后在数据库选择期间,请将其传递到您可能处理的位置或根据您的要求。

也请避免array_push()函数在循环中,使用$ss_id[] = $results[$i]->ss_id;因为它快多了。

function get_subsector($sector)
            {
                $results = $this->db->select('ss_id, ss_name')->from('account_subsector')->get()->where($sector)->result();
                $ss_id = array('-SELECT-');
                $ss_name = array('-SELECT-');
                for ($i = 0; $i < count($results); $i++)
                {
                    array_push($ss_id, $results[$i]->ss_id);
                    array_push($ss_name, $results[$i]->ss_name);
                }
                return $subsector_result = array_combine($ss_id, $ss_name);
            }

使用一些js和ajax

<script>
        $(document).ready(function(){
            $('#a_subsector').attr('disabled','disabled');
            $('#a_sector').change(function(){
                $.ajax({
                    url:'/salesforce/fill_ss',
                    type:'post',
                    data: {sector:$('#a_sector').val()},
                    success:function(result){
                        $('#a_subsector').removeAttr('disabled');
                        //$('#a_subsector').(result);
                        document.getElementById('a_subsector').innerHTML=result;
                    }
                });
                });
        });
</script>

添加一个函数到控制器

function fill_ss()
{
    $this->sf_model->get_subsector();
    $sector = $_POST['sector'];
    $data = $this->sf_model->matching($sector);
    //print_r($data);
    for ($i = 0; $i < count($data); $i++)
    {
        echo '<option value="'.$data[$i]->ss_id.'">'.$data[$i]->ss_name.'</option>';
        //echo '<option value="'.$data[$i]->ss_id.'"></option>';
        //array_push($t_id, $data[$i]->t_id);
        //array_push($t_name, $data[$i]->t_name);
    }
}

模型
function view_accounts()
{
    $results = $this->db->select('a_name,t_id,a_website,a_billingStreet,a_billingZip,a_billingCountry,a_billingCity,a_phone,a_mobile')->from('account_info')->get()->result();
    return $results;
}
function matching($match)
{
    $results =$this->db->select('ss_id,ss_name')->from('account_subsector')->where('s_id',$match)->get()->result();
    return $results;
}