如何使用Codeigniter表单验证库来验证依赖下拉列表?

时间:2019-01-22 16:28:13

标签: codeigniter

我想尝试使用CodeIgniter表单验证库进行依赖下拉验证。当我选择状态然后导致其在区域显示下拉菜单时,这是一个问题,但是这是一个问题。如果我按了提交按钮(在页面重新加载后),那么状态将保留其值,但是区域无法保留区域的值。如何解决这个问题呢。预先感谢。


控制器

<?php
class Dependent extends CI_Controller
{
    public function index()
    {
        $this->load->library("form_validation");

        if(isset($_POST["submit"]) == "Submit")
        {
            $this->form_validation->set_rules("state","State","required");
            $this->form_validation->set_rules("district","District","required");
            $this->form_validation->set_rules("name","Name","required");
            if($this->form_validation->run())
            {
                //success
            }
        }

        $this->load->model("Dependents");
        $data["state"] = $this->Dependents->get_state();
        $this->load->view("drop",$data);
    }
    public function district()
    {
        $this->load->library("form_validation");
        $this->load->model("Dependents");

        $id = $_POST["id"];
        $district = $this->Dependents->get_district($id);
        $output = "";
        $output .= "<option value=''>Select District</option>";
        foreach($district as $row)
        {
            $output .= "<option value='".$row["id"]."'>".$row["district"]."</option>";
        }
        echo $output;
    }
}
?>

模型

<?php
class Dependents extends CI_Model
{
    public function get_state()
    {
        return $this->db->get("tbl_state")->result_array();
    }
    public function get_district($id)
    {
        return $this->db->where("state",$id)->get("tbl_district")->result_array();
    }
}
?>

查看

<?php
    echo validation_errors();
?>

<form method="post" action="">
    <div class="form-group">
        <select class="form-control" id="state" name="state">
            <option value="">Select State</option>
            <?php
                foreach ($state as $row) {
                ?>
            <option value="<?php echo $row["id"]; ?>"<?php echo set_select("state", $row["id"]); ?>><?php echo $row["state"]; ?></option>
            <?php
                }
            ?>
        </select>
    </div>
    <div class="form-group">
        <select class="form-control" id="district" name=district>
            <option value="">Select District</option>
        </select>
    </div>
    <div class="form-group">
        <input type="text" name="name" placeholder="Enter Name" class="form-control">
    </div>
    <div class="form-group">
        <input type="submit" name="submit" value="Submit" class="btn btn-success btn-sm">
    </div>
</form>
<script type="text/javascript">
    $(function(){
    $("#state").change(function(){
        var id = $(this).val();

        $.ajax({
            url: "<?php echo base_url() . "Dependent/district"; ?>",
            type: "POST",
            data:{id: id},
            success: function(data)
            {
            $("#district").html(data);
            }
        });
    });
    });
</script>

2 个答案:

答案 0 :(得分:0)

尝试将name=district更改为name="district"

<select class="form-control" id="district" name="district">

答案 1 :(得分:0)

地区下拉列表不保留其值,因为它使用ajax填充并且不能使用set_select(); 仅当设置了set_value('district')时,您才可以在下拉菜单中添加一个选项。