我通过 ajax 将动态创建的表单字段发送到 CodeIgniter。
<div class="form-element">
<input type="radio" id="delete-radio[1]" name="delete-radio[1]" value="1">
</div>
<div class="form-element">
<label for="member_id[1]">Membership ID</label>
<input type="text" id="member_id[1]" name="member_id[]" autocomplete="off" value="<?php echo set_value('member_id[1]'); ?>"/>
<?php echo form_error('member_id[1]', '<div class="error">', '</div>'); ?>
</div>
<div class="form-element">
<label for="member_name[1]">Name</label>
<input type="text" id="member_name[1]" name="member_name[]" autocomplete="off" value="<?php echo set_value('member_name[1]'); ?>"/>
<?php echo form_error('member_name[1]', '<div class="error">', '</div>'); ?>
</div>
<div class="form-element">
<label for="member_address[1]">Address</label>
<input type="text" id="member_address[1]" name="member_address[]" autocomplete="off" value="<?php echo set_value('member_address[1]'); ?>"/>
<?php echo form_error('member_address[1]', '<div class="error">', '</div>'); ?>
</div>
我使用以下内容将它们发送到他们的控制器:
$('#submit').click(function() {
var form_data = $('#ajax-form').serialize();
alert(form_data);
$.ajax({
url: "<?php echo site_url('ajax_ci/ajax_check'); ?>",
type: 'POST',
async : false,
data: form_data,
success: function(msg) {
$('#form-name').append(msg);
}
});
return false;
});
这是控制器:
if ($this->input->is_ajax_request()) {
$this->form_validation->set_rules('member_id[]', 'member ID', 'trim|required|xss_clean');
$this->form_validation->set_rules('member_name[]', 'member name', 'trim|required|xss_clean');
$this->form_validation->set_rules('member_address[]', 'member password', 'trim|required|xss_clean');
if($this->form_validation->run() == FALSE) {
echo validation_errors();
} else {
// connect to database
}
} else {
echo "Work on it! Don't give up!";
}
下面的屏幕截图是当前发生的情况。
但我希望这些错误出现在他们各自的个人身上form_errors
在我当前的设置中这可能吗?