1

我收集国家和州的代码,我从选择标签中选择一个国家名称,并在国家下所有州显示在下一个选择框中。所有数据均来自 MySQL,并由 jQuery、ajax 处理。

我无法从控制器获得价值。为什么?

$data=$this->input->post('state',true) 

我的代码如下:模型中的 CI

<?php
/* File : ajax.php
 * Author : Manish Kumar Jangir
*/
class AJAX {

    private $database = NULL;
    private $_query = NULL;
    private $_fields = array();
    public  $_index = NULL;
    const DB_HOST = "localhost";
    const DB_USER = "root";
    const DB_PASSWORD = "";
    const DB_NAME = "csc";


    public function __construct(){
        $this->db_connect();                    // Initiate Database connection
        $this->process_data();
    }

    /*
     *  Connect to database
    */
    private function db_connect(){
        $this->database = mysql_connect(self::DB_HOST,self::DB_USER,self::DB_PASSWORD);
        if($this->database){
            $db =  mysql_select_db(self::DB_NAME,$this->database);
        } else {
            echo mysql_error();die;
        }
    }

    private function process_data(){
        $this->_index = ($_REQUEST['index'])?$_REQUEST['index']:NULL;
        $id = ($_REQUEST['id'])?$_REQUEST['id']:NULL;
        switch($this->_index){
            case 'country':
                $this->_query = "SELECT * FROM countries";
                $this->_fields = array('id','country_name');
                break;
            case 'state':
                $this->_query = "SELECT * FROM states WHERE country_id=$id";
                $this->_fields = array('id','state_name');
                break;
            case 'city':
                $this->_query = "SELECT * FROM cities WHERE state_id=$id";
                $this->_fields = array('id','city_name');
                break;
            default:
                break;
        }
        $this->show_result();
    }

    public function show_result(){
        echo '<option value="">Select '.$this->_index.'</option>';
        $query = mysql_query($this->_query);
        while($result = mysql_fetch_array($query)){
            $entity_id = $result[$this->_fields[0]];
            $enity_name = $result[$this->_fields[1]];
            echo "<option value='$entity_id'>$enity_name</option>";
        }
    }
}

$obj = new AJAX;

?>

这是我的看法:

<html>
<head>
<title>Country State City Dependent Dropdown using Ajax</title>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery-1.5.2.min.js"></script>
<style type="text/css">
body{
    font-family:arial;
    color:#666;
}
form select{
    padding:5px;
    border:1px solid #aaa;
    border-radius:4px;
    width:120px;
}

form label{
    font-size:12px;
    font-weight:bold;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
    load_options('','country');
});

function load_options(id,index){
    $("#loading").show();
    if(index=="state"){
        $("#city").html('<option value="">Select city</option>');
    }
    $.ajax({
        url: "<?php echo base_url(); ?>index.php/welcome/includ.html?index="+index+"&id="+id,
        complete: function(){$("#loading").hide();},
        success: function(data) {
            $("#"+index).html(data);
        }
    })
}
</script>
</head>
<body>
<div style="width:800px; margin:auto;padding-top:100px;">
<h1>Country,State,City dynamic dependent dropdown using Ajax and Jquery</h1>
<form action="<?php echo base_url(); ?>index.php/city_ajax/show_result" method="post">
        <label>Select Country</label>
        <select id="country" onchange="load_options(this.value,'state');">
            <option value="">Select country</option>
        </select>
        &nbsp;&nbsp;&nbsp;
        <label>Select State</label>
        <select id="state" onchange="load_options(this.value,'city');">
            <option value="">Select state</option>
        </select>
        &nbsp;&nbsp;&nbsp;
        <label>Select city</label>
        <select id="city">
            <option value="">Select City</option>
        </select>
        <img src="<?php echo base_url(); ?>loader.gif" id="loading" align="absmiddle" style="display:none;"/>
</form>
</div>

</body>
</html>
4

2 回答 2

0

正如你所说的 CI 模型:

在 CodeIgniter 中,您必须将 CI_Model 扩展为:

  class Exam_model extends CI_Model {}

为了使它成为 CI 模型,它只是一个 .php 文件

对这个

    $data=$this->input->post('state',true);

您正在尝试评估需要将ajax 类型作为 POST的post参数

     url: "<?php echo base_url(); ?>index.php/welcome/includ.html?index="+index+"&id="+id,
    complete: function(){$("#loading").hide();},
    type:"POST", // you have to mention post type here
    success: function(data) {
        $("#"+index).html(data);
    } 

希望它会有所帮助!

于 2013-03-16T08:18:25.023 回答
0

选择语句:

country_id=$id应该 country_id='" . $id . "'

state_id=$id应该 state_id='" . $id . "'

于 2013-03-16T08:10:16.233 回答