使用链接选择与AJAX,PHP& MVC

时间:2017-08-24 12:33:10

标签: php jquery codeigniter

所以我想创建一个带有链接链接的表单,选择一个国家/地区会给您一个州和一个州给一个城市。我已经设置了我的数据库和表格,并且我已经编写了我的ajax代码。国家列表出现良好,但在选择国家/地区后,ajax无法选择country_id来填充州字段,反之亦然。 我使用类似于codeigniter的mvc框架,我需要帮助。

这是我在视图中的ajax调用和html:

<script type="text/javascript">
$(document).ready(function(){
    $('#country').on('change',function(){
        var countryID = $(this).val();
        if(countryID){
            $.ajax({
                type:'POST',
                url:'<?php echo URL?>patient_appointment/getstate',
                data:'country_id='+countryID,
                success:function(html){
                    $('#state').html(html);
                    $('#city').html('<option value="">Select state first</option>'); 
                }
            }); 
        }else{
            $('#state').html('<option value="">Select country first</option>');
            $('#city').html('<option value="">Select state first</option>'); 
        }
    });

    $('#state').on('change',function(){
        var stateID = $(this).val();
        if(stateID){
            $.ajax({
                type:'POST',
                url:'<?php echo URL?>patient_appointment/getcity',
                data:'state_id='+stateID,
                success:function(html){
                    $('#city').html(html);
                }
            }); 
        }else{
            $('#city').html('<option value="">Select state first</option>'); 
        }
    });
});
  </script>
 <select name="country" id="country">
        <option value="">Select Country</option>
            <?php if ($total_countries > 0) { foreach ($country as $key => $value) { ?>
                            <option value="<?php echo $value['country_id'] ?>"><?php echo $value['country_name'] ?></option>
                            <?php }} else { echo "<option value=''>No data in database</option>"; } ?>
                        </select>

    <select name="state" id="state">

        <option value="">Select country first</option>
    </select>

    <select name="city" id="city">
        <option value="">Select state first</option>
    </select>
                        <div name="state" id="state"></div>

这是我的控制器

function index() 
    {   
        $this->view->data['country']=$this->model->getcountries();
        $this->view->data['total_countries']=$this->model->gettotalcountries();
        if (isset($_POST['country_id'])) {
            $country_id = $_POST['country_id'];

        $this->view->data['info1']=$this->model->getdata1($country_id);
        $this->view->data['total_info1']=$this->model->gettotaldata1($country_id);
    }
        $this->view->render('patient_appointment/index', $noinclude=false, 2);
    }

    function getstate()
    {

        if(isset($_POST["country_id"]) && !empty($_POST["country_id"])){
            $country_id=$_POST['$country_id'];
            $this->view->data['total_states']=$this->model->gettotalstates($country_id);
            $this->view->data['state']=$this->model->getstates($country_id); 

        }
    }

这是我的模型

public function __construct()
    {
        parent::__construct();
    }
    public function getcountries()
    {
        $sth = $this->db->query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC");
            $rowss=$sth->rows;
            return $rowss;
    }
    public function gettotalcountries()
    {
        $sth = $this->db->query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC");
            $rowss=$sth->num_rows;
            return $rowss;
    }
    public function getstates($country_id)
    {
        $sth = $this->db->query("SELECT * FROM states WHERE country_id = '$country_id' AND status = 1 ORDER BY state_name ASC");
            $rowss=$sth->rows;
            return $rowss;
    }
    public function gettotalstates($country_id)
    {
        $sth = $this->db->query("SELECT * FROM states WHERE country_id = '$country_id' AND status = 1 ORDER BY state_name ASC");
            $rowss=$sth->num_rows;
            return $rowss;
    }

请问我做错了什么?

1 个答案:

答案 0 :(得分:2)

这条线

$country_id=$_POST['$country_id'];

$country_id=$_POST['country_id'];

或实际上the CI method

$country_id = $this->input->post('country_id');