我在数据库中插入一个表单,描述很重要,所以我想要textarea字段。 控制器
function get()
{
$data =array('success' => false,'message' =>array());
$this -> form_validation -> set_rules('msg', 'Description', 'trim|required|max_length[1000]');
$this -> form_validation -> set_rules('feed_title', 'Feed Title', 'required');
$this -> form_validation -> set_rules('feed_link', 'Feed Link', 'required');
$this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');
if($this->form_validation->run()== false)
{
foreach($_POST as $key => $value)
{
$data['message'][$key] = form_error($key);
}
}
else
{
$data['success']=true;
}
echo json_encode($data);
}
表单页面
<!-- Modal -->
<div id="add_feed" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div id="feed_success"></div>
<form method="post" id="feed_form" action="<?php echo base_url()?>welcome/get">
<div class="modal-body">
<div class="form-group clearfix">
<label>Feed Title</label>
<input type="text" class="form-control" name="feed_title" id="feed_title" placeholder="Enter Feed Title"/>
</div>
<div class="form-group clearfix">
<label>Feed Link</label>
<input type="text" class="form-control" name="feed_link" id="feed_link" placeholder="Enter Feed Link"/>
</div>
<div class="form-group clearfix">
<label>Feed Short Description</label>
<textarea class="textarea form-control" placeholder="Message" name="msg" style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Add Feed</button><button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
Ajax功能
<script>
$(document).ready(function(){
$(".textarea").wysihtml5({});
$('#feed_form').on('submit',function(form){
form.preventDefault();
var me = $(this);
$.ajax({
url:me.attr('action'),
type:'post',
data:me.serialize(),
dataType:'json',
success:function(response){
if(response.success == true){
$("#feed_form")[0].reset();
$('#feed_success').addClass('alert alert-success').html("Feed Add Successfully!");
$('div.form-group').removeClass('alert alert-danger').find('.text-danger').remove();
$('div.form-group').removeClass('alert alert-success').find('.text-success').remove();
}
else{
$.each(response.message,function(key,value){
var element = $("#"+key);
element.closest('div.form-group').removeClass('alert alert-danger')
.addClass(value.length > 0 ? 'alert alert-danger' : 'alert alert-success').find('.text-danger').remove();
element.after(value);
});
}
}
});
});
});
</script>
对于其工作的其他两个输入,“必需”不起作用。