Php 无法识别Codeigniter下拉列表验证

Php 无法识别Codeigniter下拉列表验证,php,codeigniter,Php,Codeigniter,我是codeigniter的新手,需要创建一个简单的下拉菜单来识别一个选项在单击“提交”后没有被选中。我的下拉菜单工作正常,但没有验证(没有选择选项时无法识别)。我使用了form_验证,但是我似乎找不到代码的问题。你能提供一些关于如何解决这个问题的见解吗?谢谢大家! 控制器(User.php): 在用户控制器中更换 $this->form_validation->set_rules('cities', 'Tool', 'callback_validate_dropdown'); 到

我是codeigniter的新手,需要创建一个简单的下拉菜单来识别一个选项在单击“提交”后没有被选中。我的下拉菜单工作正常,但没有验证(没有选择选项时无法识别)。我使用了form_验证,但是我似乎找不到代码的问题。你能提供一些关于如何解决这个问题的见解吗?谢谢大家!

控制器(User.php):


在用户控制器中更换

$this->form_validation->set_rules('cities', 'Tool', 'callback_validate_dropdown');

取而代之

<select id='sel_city'>
       <option>-Select-</option>

-挑选-


-挑选-

查看验证工作正常

您的
select
元素都没有名称属性,因此它们都没有被发送到服务器。@Kisaragi名称属性是什么意思?在my user_view.php中?是的,在您的视图中,您提交给
用户/索引的每个select元素必须有一个名称元素:
..
名称属性是
$\u POST['somename']
工作的方式。您选择的名称如下:
<!doctype html>
<html>
<body>

   <table border='0'>


     <!-- City -->
     <tr>
       <td>Tool</td>
       <td>
         <select id='sel_city'>
           <option>-Select-</option>
           <?php
           foreach($cities as $city){
             echo "<option value='".$city['id']."'>".$city['cityname']."</option>";

           }
           ?>
        </select>
      </td>
    </tr>



    <!-- Department -->
    <tr>
      <td>Tool Version</td>
      <td>
        <select id='sel_depart'>
          <option>-Select-</option>
        </select>
      </td>
    </tr>

    <!-- User -->
    <tr>
      <td>Corresponding Tool JSON Sent To Pachyderm</td>
      <td>
        <select id='sel_user'>
          <option>-Select-</option>
        </select>
      </td>
    </tr>



    <!-- Dataset -->
     <tr>
       <td>Dataset</td>
       <td>
         <select id='sel_dataset'>
           <option>-Select-</option>
           <?php
           foreach($datasets as $dataset){
             echo "<option value='".$dataset['id']."'>".$dataset['datasetname']."</option>";
           }
           ?>
        </select>
      </td>
    </tr>


    <!-- Dataset Version -->
    <tr>
      <td>Dataset Version</td>
      <td>
        <select id='sel_dsversion'>
          <option>-Select-</option>
        </select>
      </td>
    </tr>


    <!-- Version User JSON -->
    <tr>
      <td>Corresponding Dataset JSON's Sent To Pachyderm</td>
      <td>
        <select id='sel_user_version'>
          <option> -Select- </option>
        </select>
      </td>
    </tr>
  </table>

  <!-- Script -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <script type='text/javascript'>
  // baseURL variable
  var baseURL= "<?php echo base_url();?>";

  $(document).ready(function(){

    // City change
    $('#sel_city').change(function(){
      var city = $(this).val();

      // AJAX request
      $.ajax({
        url:'<?=base_url()?>index.php/User/getCityDepartment',
        method: 'post',
        data: {city: city},
        dataType: 'json',
        success: function(response){

          // Remove options 
          $('#sel_user').find('option').not(':first').remove();
          $('#sel_depart').find('option').not(':first').remove();

          // Add options
          $.each(response,function(index,data){
             $('#sel_depart').append('<option value="'+data['id']+'">'+data['depart_name']+'</option>');
          });
        }
     });
   });



    // Dataset change
    $('#sel_dataset').change(function(){
      var dataset = $(this).val();

      // AJAX request
      $.ajax({
        url:'<?=base_url()?>index.php/User/getDatasetVersion',
        method: 'post',
        data: {dataset: dataset},
        dataType: 'json',
        success: function(response){

          // Remove options 
          $('#sel_user_version').find('option').not(':first').remove();
          $('#sel_dsversion').find('option').not(':first').remove();

          // Add options
          $.each(response,function(index,data){
             $('#sel_dsversion').append('<option value="'+data['id']+'">'+data['version_name']+'</option>');
          });
        }
     });
   });



    // Department change
   $('#sel_dsversion').change(function(){
     var dsversion = $(this).val();

     // AJAX request
     $.ajax({
       url:'<?=base_url()?>index.php/User/getVersionUsers',
       method: 'post',
       data: {dsversion: dsversion},
       dataType: 'json',
       success: function(response){

         // Remove options
         $('#sel_user_version').find('option').not(':first').remove();

         // Add options
         $.each(response,function(index,data){
           $('#sel_user_version').append('<option value="'+data['id']+'">'+data['name']+'</option>');
         });
       }
    });
  });





   // Department change
   $('#sel_depart').change(function(){
     var department = $(this).val();

     // AJAX request
     $.ajax({
       url:'<?=base_url()?>index.php/User/getDepartmentUsers',
       method: 'post',
       data: {department: department},
       dataType: 'json',
       success: function(response){

         // Remove options
         $('#sel_user').find('option').not(':first').remove();

         // Add options
         $.each(response,function(index,data){
           $('#sel_user').append('<option value="'+data['id']+'">'+data['name']+'</option>');
         });
       }
    });
  });



 });
 </script>





<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CodeIgniter Dropdown from Database Example</title>
    <link href="<?php echo base_url("assets/bootstrap/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
    <style>
    .well {
        background: none;
    }
    </style>
</head>

<body>


    <div class="col-xs-6 col-xs-offset-3 well">
        <?php $attributes = array("name" => "booksform");
        echo form_open("User/index", $attributes);?>

        <div class="form-group">
            <button name="submit" type="submit" class="btn btn-info">Submit</button>

        </div>
        <?php echo form_close(); ?>
    </div>
</div>
</div>

</body>



 </body>
</html>
$this->form_validation->set_rules('cities', 'Tool', 'callback_validate_dropdown');
$this->form_validation->set_rules('sel_city', 'Tool', 'required');
<select id='sel_city'>
       <option>-Select-</option>
<select id='sel_city' name="sel_city">
       <option value="">-Select-</option>