【问题标题】:Codeigniter form validation to check whether a value of input has changedCodeigniter 表单验证以检查输入值是否已更改
【发布时间】:2016-03-11 14:01:28
【问题描述】:

我正在使用codeigniter 3.0.4,但现在遇到了一个基本问题。我有一个<input type='text'>,其默认值是使用 Jquery 生成的。

我的问题,我提交表单时如何检查值是否已更改?

如果现在的值与默认给定值不同,它将进行回调验证(实际上是检查电子邮件可用性)。否则,如果该值仍然与默认给定值相同,它将跳过回调验证。


这是我的模态

<div class="modal-body" id="myModalBody">
   <form id="contactform" role="form" method="POST" action='<?php echo  base_url('administrator/kategori/editcategory');?>' >
       <div class="form-group">
           <label for="kategori"> Nama Kategori </label>
           <input type="hidden" name="id" class='id' id='id'>
           <input type="text" class="form-control edit-category" name='edit-category' id="edit-category" required autofocus>
       </div>
</div>
<div class="modal-footer">
    <button type="submit" class="btn btn-success confirm-edit"> Edit Data </button>
    </form>
</div>

如您所见,上面的表单将数据提交给administrator/kategori/editkategori

这是我的editkategori() 函数

public function editcategory(){
 if($this->input->post()){
 $this->form_validation->set_rules('kategori', 'Kategori Baru', 'required|callback_correctcategory');

     if($this->form_validation->run() == FALSE){
        $this->data['result']   = $this->admincrud->getcategorylist();
        $this->data['categoryname'] = $this->admincrud->fetchcategoryname();
        $this->load->view($this->layout, $this->data);  

     } else {

        $tobesent = array(
           "id" => $this->input->post('id'),
           "kb" => $this->input->post('kategori')
        );

        $result = $this->admincrud->editcategory($tobesent);
        if($result){
          $this->session->set_flashdata('result', 'Kategori Sukses Diubah');
          redirect('administrator/kategori');
        }

     }

 } else {

    redirect('administrator/kategori');

 }

}

这是JQuery 函数,用于在显示上述模式时自动为相应的文本赋值

$('.toggle-edit-modal').click(function(){

  var id           = $(this).closest('tr').find('td:eq(0)').text();
  var categoryname = $(this).closest('tr').find('td:eq(1)').text();

  $('.id').val(id);
  $('.edit-categoryname').val(categoryname);

})

在验证规则集中,我放了回调correctcategory(),这就是correctcategory()函数:

public function correctcategory($str){

    $sterilizedkey  = strtoupper(trim(str_replace(" ", "", $str)));
    $tobeshown      = ucfirst(strtolower($sterilizedkey));
    $this->CI->db->select("upper(trim(REPLACE(`CategoryName`, ' ',''))) as `CategoryName`");
    $this->CI->db->from('category');
    $result = $this->CI->db->get()->result_array();
    $data   = array();
    foreach ($result as $key => $value) {
        $data[] = $value['CategoryName'];
    }
    if(in_array($sterilizedkey, $data)){
        $this->set_message('correctcategory', 'The faculty has been registered before');
        return false; 
    } else {
        return true;
    }
}

使用上面的代码,系统将评估通过表单提交的每个值。

当我打开模式时出现问题,并出现一个带有默认给定值的文本框。如果我直接提交表单但不更改文本框的值,或者当新值和旧给定值完全相同时,如何跳过correctcategory 验证?

【问题讨论】:

  • 到目前为止你做了什么?你能显示一些代码吗?
  • 已更新代码。请检查我的代码
  • 在您的 PHP 中,您正在验证名称为 kategori 的输入,但我在您的 HTML 中看不到该输入。
  • 你说这个 "...或者当新值和旧值给定一个..." 但是你说的旧值是什么意思?我根本没有看到您正在预设 HTML 输入。这意味着这些输入永远不会有“旧值”,它们只会是空白

标签: javascript php jquery codeigniter validation


【解决方案1】:

您必须将原始值存储在隐藏的文本框中。并使用具有值的文本框进行检查。

例如:

<input type="text" id="orig" value="your email">
<input type="hidden" id="hid" value="your email">

然后在 jquery 中提交检查,如下所示

var original = $('#orig').val();
var static = $('#hid').val();
if(original == static){
  alert("not changed");
}else{
  alert("changed");
}

注意:此代码仅作为示例,因为您没有共享任何代码

【讨论】:

    【解决方案2】:

    如果你知道 Jquery 设置的值,那么控制器也应该知道(猜测这是类别数据库值) 基于默认值已知的假设。 为了解决这个问题,控制器函数需要知道您为输入设置的默认值。

    class Categories extends CI_Controller {
    
    public function edit()
    {
    
    
       // form submited?
       If ( $this->input->post()) 
       {
           $this->load->model('category_model');
    
           $error = null;
           $success = false ;
    
            $id = $this->input->post('id');
            // now you were looking for 'kategori' instead of 'edit-category'
            $posted_cat_name = $this->input->post('edit-category'); 
            // get the db record
            $category = $this->category_model->fetch( array('id' => $id ));
    
            if( !$category->id)
            {
               $error = 'Category not exist' ;
               // set flash and redirect
                redirect('categories/admin_list');
            }
           // category remain the same
           If( $posted_cat_name ==  $category->name)
           {
              $error = "No changes made" ;
           }
           else if( $this->category_model->update($id , $posted_cat_name) == false)
           {
              $error = 'Error occured updating category' ;
              // get the error and log_message() for debuging
           }
           else
           {
              $success = true ;
              // set flash data and redirect
    
            }
    
    
      }
      // do you need this remaining code? aren't you suppose to redirect back to admin list where the form was posted  ?
       $this->data['result']   = $this->admincrud->getcategorylist();
       $this->data['categoryname'] = $this->admincrud->fetchcategoryname();
      $this->load->view($this->layout, $this->data);  
      }
     }
    

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 1970-01-01
      • 2011-02-20
      • 2020-03-08
      • 1970-01-01
      • 2023-03-17
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多