真的需要帮助。我似乎无法验证此页面的工作。我有一个显示类别的页面,一切正常。添加和编辑记录没有问题,除了我检查记录是否已存在的验证不起作用。
查看:
<?php echo validation_errors(); ?>
<?php
$message = (isset($is_editing))? 'Name already exists' : 'Name available';
$mode = (isset($is_editing))? 'disabled' : 'disabled';
$category_name = (isset($is_editing))? $categorycontent->category_name : NULL;
$process = (isset($is_editing))? 'Edit' : 'Create';
$form_action = (isset($is_editing))? 'category/update_category/' . $id : 'category/create_category';
$test = NULL;
?>
<style type="text/css">
.check_exists
{
margin-top: 4px;
margin-left: 9px;
position: absolute;
width: 16px;
height: 16px;
}
</style>
<?php echo form_open($form_action, 'id="frm" class="form-horizontal" style="margin: 0;"'); ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h4 class="modal-title"><?php echo $process ?> Item Category Information</h4>
</div>
<div class="span6" style="padding-top:20px;padding-bottom:10px;">
<!-- Project Content Fields -->
<div class="control-group">
<p class="control-label">Item Category Name*</p>
<div class="controls">
<?php echo form_input('category_name', $category_name, 'id="category_name" style="height: 30px;"'); ?><span id="team_verify" class="check_exists" ></span>
<?php echo form_input('test', $test, 'id="test" style="height: 30px;"') ?>
</div>
</div>
<!-- <div class="control-group">
</div> -->
<!-- <?php echo form_hidden('creator_id', $this->ion_auth->user()->row()->id); ?> -->
<!-- END Project Content Fields -->
</div>
<?php echo form_close(); ?>
<script type="text/javascript">
$(document).ready(function() {
$("#category_name").keyup(function() {
//remove all the class add the messagebox classes and start fading
$("#team_verify").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
//check the category_name exists or not from ajax
var name = $("#category_name").val();
$.ajax({
url : "<?=base_url()?>category/check_availability",
data : { category_name : name },
type : 'POST',
success : function(data){
if( data == '0' ){
$("#team_verify").fadeTo(200,0.1,function() { //start fading the messagebox
//add message and change the class of the box and start fading
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
});
}else if( data == '1' ){
$("#team_verify").fadeTo(200,0.1,function() { //start fading the messagebox
//add message and change the class of the box and start fading
$(this).html('Category_name available to register').addClass('messageboxok').fadeTo(900,1);
});
}else{
alert("Some error occured! Check Console");
}
}
});
});
});
</script>
还有我的 类别 Controller :
public function check_availability() {
$category_name = $this->input->post('category_name');
$this->form_validation->set_rules('category_name', 'category_name', 'required|is_unique[tblitemcategories.category_name]');
if ($this->form_validation->run() == FALSE) {
//user name is not availble
echo "no";
} else {
//user name is available
echo "yes";
}
}
我不知道自己做错了什么,而且我对创建网站还很陌生,所以非常感谢任何帮助。非常感谢。
最佳答案
我认为您必须将 echo 语句值更改为 0/1 而不是 no/yes。
public function check_availability() {
$category_name = $this->input->post('category_name');
$this->form_validation->set_rules('category_name', 'category_name', 'required|is_unique[tblitemcategories.category_name]');
if ($this->form_validation->run() == FALSE) {
//user name is not availble
echo "0";
} else {
//user name is available
echo "1";
}
关于PHP CodeIgniter 框架中的 Javascript 可用性验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26998945/