Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter回调下拉列表表单验证_Codeigniter - Fatal编程技术网

Codeigniter回调下拉列表表单验证

Codeigniter回调下拉列表表单验证,codeigniter,Codeigniter,只想在将所选国家添加到数据库之前验证我的codeigniter下拉列表。问题是它将默认值“selectcountry”插入数据库,而不是显示消息“Please choose your country.”请帮助我:) 这是我的型号: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Model_example extends CI_Model { function __c

只想在将所选国家添加到数据库之前验证我的codeigniter下拉列表。问题是它将默认值“selectcountry”插入数据库,而不是显示消息“Please choose your country.”请帮助我:)

这是我的型号:

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

class Model_example extends CI_Model {

  function __construct()
 {
  //Call the Model constructor
   parent::__construct();
 }

public function did_add() {

        $data = array(
        'country' => $this->input->post('country')                 
                     );     
        $query = $this->db->insert('example_table', $data);

        if ($query) {
            return true;} 
        else {
            return false;}      
    }  
}
  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Example extends MX_Controller {

    public function index() {       
            $data["message"] = "";            
            $this->load->view("view_example",$data);        
               }

    public function add() {                     
            $this->load->library('form_validation');
            $this->load->model('model_example');            
            $this->form_validation->set_rules('country', 'Country', 'required|callback_country_check');     
            if($this->form_validation->run()){
            $this->model_example->did_add();    
            }
            else {
            $data["message"] = "";          
            $this->load->view("view_example",$data);               
            }       
           }

     public function country_check()
    {
            if ($this->input->post('country') === 'selectcountry')  {
            $this->form_validation->set_message('country_check', 'Please choose your country.');
            return FALSE;
        }
        else {
            return TRUE;
        }
    }
}

在查看页面中更改此行

$data = array(
          "selectcountry"  => "Select Country",               
          "CA" => "Canada",
          "US" => "United States",
          "ZW" => "Zimbabwe"
                  );
  echo form_dropdown('country', $data, 'selectcountry');
对此

$data = array(
          ""  => "Select Country",               
          "CA" => "Canada",
          "US" => "United States",
          "ZW" => "Zimbabwe"
                  );
  echo form_dropdown('country', $data, set_value('country'));

您不需要另一个回调函数来检查值是否为空。

非常感谢您的帮助,它按照您描述的方式工作:)但是,我还想知道我应该在代码中做哪些更改以使回调函数工作…而不是
if($this->input->post('country')=='selectcountry')
尝试添加
if($this->input->post('country')=='selectcountry')($this->input->post('country')=='selectcountry')
来检查条件。我不确定,但它应该可以工作。
$data = array(
          ""  => "Select Country",               
          "CA" => "Canada",
          "US" => "United States",
          "ZW" => "Zimbabwe"
                  );
  echo form_dropdown('country', $data, set_value('country'));