只是想问一个问题,如何在弹出对话框中通过 codeigniter 表单验证。现在我创建了一个表单,其中包含类别列表以及编辑和删除按钮。该场景是当成员单击更新按钮时,它将弹出一个对话框,显示一个添加新类别表单。实际上我在弹出窗口中加载了我的 html 表单。我的问题是,如果用户输入了重复的类别名称,则表单验证应该出现在弹出对话框中。就我而言,它将打开一个新页面,将我的表单与验证错误一起显示。如何在弹出 jquery 中显示表单验证?这就是所有的人,谢谢。
这是我的一些代码(我使用 Codeigniter)
//homepage.php(显示分类详情列表)
<?php $data_var = array('name'=>'category','id'=>'addForm'); ?>
<?php echo form_open('category_controller/delete_category',$data_var); ?>
<input class="classname1" type="button" value="ADD" name="add_category"/> <!-- button for add -->
<!-- BLOW IS MY FOREACH FOR DISPLAYING INFO -->
.
.
.
foreach($queryViewEntries as $row){
$temp_id = $row['salescatid'];
echo "<tr>";
echo "<td width='5%' style='text-align: center'><input type='checkbox' name='category[]' value=".$row['salescatid']." id=check".$row['salescatid']." onclick='check(this)' /></td>";
echo "<td width='10%'>{$row['salescatname']}</td>";
echo "<td>{$row['salescatdesc']}</td>";
?>
<td width="15%" style="text-align: center;">
<?php
$cat_id = $row['salescatid'];
?>
<input type="hidden" name="cat_id" value="<?php echo $cat_id; ?>" />
<input type="button" name="edit_item" data-value="<?php echo $cat_id; ?>" value="EDIT" />
<input type="button" name="delete_item" value="DELETE" data-value="<?php echo $cat_id; ?>" />
</td>
<?php
echo "</tr>";
}
....
<!-- MY DIV -->
<div id="popup" style="display: none;"></div>
<div id="edit" style="display: none"></div>
<div id="delete" style="display: none;"></div>
....
....
<!-- BELOW IS MY JQUERY FUNCTION THAT WILL CALL THE POP UP AND LOAD MY HTML FORM INSIDE IT.
$(function(){
/* FOR ADD PAGE */
$(".hero-unit input[name=add_category]").on('click',function(){
$('#popup').load("<?php echo site_url("category_controller/addCategoryItem/"); ?>").dialog({
title: "Add New Category",
autoOpen: true,
width: 450,
modal:true,
open: function (event, ui) { window.setTimeout(function () {
jQuery(document).unbind('mousedown.dialog-overlay').unbind('mouseup.dialog-overlay'); }, 100);
},
close: function() {
$('#popup').dialog("close");
}
});
});
//category_controller.php(我的控制器)
<?php
class Category_Controller extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function addCategoryItem(){
$this->form_validation->set_rules('name','Category Name','required|is_unique[sales_category.salescatname]');
$this->form_validation->set_rules('description','Description','required');
if($this->form_validation->run() == FALSE){
$data['title'] = "Add Item";
$data['copyright'] = date('Y');
$this->load->view('User/header/header_user',$data);
$this->load->view('Item/contents/addNewItem');
}else{
$this->category_model->addCategory();
$this->load->view('Item/contents/duccessAdd');
}
}
}
//controller_model.php(我的模型函数)
public function addCategory(){
$cname = ucwords($this->input->post('name'));
$cdesc = $this->input->post('description');
$insertCategory = array(
'salescatid' => NULL,
'salescatname' => $cname,
'salescatdesc' => $cdesc,
);
$result = $this->db->insert('sales_category',$insertCategory);
if($result){
return TRUE;
}else{
return FALSE;
}
}
//addNewItem.php(我添加新类别的表单)
<?php $attr = array('class'=>'form-signin','id'=>'addForm'); ?>
<?php echo form_open('category_controller/insertItem',$attr); ?>
<h2 class="form-signin-heading"></h2>
<h5 style="font-weight: normal;">Category Name:</h5>
<input type="text" class="input-block-level" placeholder="Category Name" required="required" name="name" autofocus="autofocus" value="<?php echo set_value('name'); ?>"/>
<label style="color: red;"><?php echo form_error('name'); ?></label>
<h5 style="font-weight: normal;">Desciption</h5>
<input type="text" class="input-block-level" placeholder="Description" required="required" name="description" value="<?php echo set_value('description'); ?>" />
<label style="color: red;"><?php echo form_error('description'); ?></label>
<br />
<div align="right">
<input type="submit" value="OK" class="btn btn-large btn-primary" />
<input type="button" value="CANCEL" class="btn btn-large btn-primary" name='cancel' />
</div>
<?php echo form_close(); ?>
这是我的代码示例,希望您能帮助我。谢谢。