Javascript Codeigniter表单验证,以检查输入值是否已更改
我正在使用Javascript Codeigniter表单验证,以检查输入值是否已更改,javascript,php,jquery,codeigniter,validation,Javascript,Php,Jquery,Codeigniter,Validation,我正在使用codeigniter 3.0.4,现在遇到了一个基本问题。我有一个带有使用Jquery生成的默认值的 我的问题是,提交表单时如何检查值是否已更改 如果现在的值与默认的给定值不同,它将执行回调验证(实际上是检查电子邮件可用性)。否则,如果该值仍然与默认给定值相同,它将跳过回调验证 这是我的情态 <div class="modal-body" id="myModalBody"> <form id="contactform" role="form" method=
codeigniter 3.0.4
,现在遇到了一个基本问题。我有一个带有使用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>
这是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
验证?必须将原始值存储在隐藏的文本框中。并使用包含值的文本框进行检查
例如:
<input type="text" id="orig" value="your email">
<input type="hidden" id="hid" value="your email">
注意:此代码仅作为示例,因为您没有共享任何代码您必须将原始值存储在隐藏的文本框中。并使用包含值的文本框进行检查 例如:
<input type="text" id="orig" value="your email">
<input type="hidden" id="hid" value="your email">
注意:此代码仅用于示例,因为您没有共享任何代码如果您知道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);
}
}
如果您知道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);
}
}
到目前为止你做了什么?你能显示一些代码吗?用代码更新。请在您的PHP中检查我的代码。您正在验证名为
kategori
的输入,但我在您的HTML中没有看到该输入。您这样说“……或者当新值和旧值同时出现时……”但旧值是什么意思?我看不出您在预设HTML输入。这意味着这些输入永远不会有“旧值”,它们只是空白。到目前为止,您做了什么?你能显示一些代码吗?用代码更新。请在您的PHP中检查我的代码。您正在验证名为kategori
的输入,但我在您的HTML中没有看到该输入。您这样说“……或者当新值和旧值同时出现时……”但旧值是什么意思?我看不出您在预设HTML输入。这意味着这些输入永远不会有“旧值”,它们只是空白