正在使用twitter bootstrap和代码点火器进行应用程序。我有一个使用bootstrap模式开发的表单,并使用ajax将数据从视图传递到控制器。但不幸的是,在提交表单时,它不会将数据存储在数据库中。
模型
public function get_primary()
{
$this->db->select('primary_id');
$this->db->select('category');
$this->db->from('primary_category');
$query = $this->db->get();
$result = $query->result();
//array to store primary id & category
$prim_id = array('Select Primary Category');
$cate = array('Select Primary Category');
for ($i = 0; $i < count($result); $i++) {
array_push($prim_id, $result[$i]->primary_id);
array_push($cate, $result[$i]->category); }
return $primary_result = array_combine($prim_id, $cate);
}
查看
<script type="text/javascript">
$('#submit').click(function() {
var form_data = {
title: $('#title').val(),
pcategory: $('#pcategory').val(),
scategory: $('#scategory').val(),
sdescription: $('#sdescription').val(),
description: $('#description').val(),
benefits: $('#benefits').val(),
hours: $('#hours').val()
area: $('#area').val()
location: $('#location').val()
dateline: $('#dateline').val()
vacancies: $('#vacancies').val()
};
$.ajax({
url: "<?php echo site_url('Company/profilesearch'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
if (msg == 'YES')
$('#alert-msg').html('<div class="alert alert-success text-center">Posted successfully!</div>');
else if (msg == 'NO')
$('#alert-msg').html('<div class="alert alert-danger text-center">Error while submiting your internship.</div>');
else
$('#alert-msg').html('<div class="alert alert-danger">' + msg + '</div>');
}
});
return false;
});
</script>
&#13;
<div class="modal fade modal-success clearfix" id="postNewInternship" aria-hidden="true" aria-labelledby="postNewInternship" role="dialog" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Post A new internship.</h4>
</div>
<div class="modal-body">
<?php echo form_open('Company/profilesearch');?>
<div class="form-group">
<input type="text" id="tittle" name="tittle" class="form-control" value="" required="required" pattern="" title="" placeholder="Tittle of the internship">
<span class="text-danger small"><?php echo form_error('tittle'); ?></span>
</div>
<div class="form-group">
<?php $attributes = 'class = "form-control" id = "pcategory"'; echo form_dropdown('primary',$primary,set_value('primary'),$attributes);?>
<span class="text-danger small"><?php echo form_error('pcategory'); ?></span>
</div>
<div class="form-group">
<?php $attributes = 'class = "form-control" id = "scategory"';
echo form_dropdown('secondary',$secondary,set_value('secondary'),
$attributes);?>
<span class="text-danger small"><?php echo form_error('scategory'); ?></span>
</div>
<div class="form-group">
<textarea class="form-control" id="description" name="sdescription" rows="3" placeholder=" Enter short description of your internship for search..must be 140 words."></textarea>
<span class="text-danger small"><?php echo form_error('scategory'); ?></span>
</div>
<div class="form-group">
<textarea class="form-control" id="description" name="description" rows="3" placeholder="Enter your description of your internship..must be 500+ words."></textarea>
<span class="text-danger small"><?php echo form_error('description'); ?></span>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<?php $attributes = 'class = "form-control" id = "benefits"';
echo form_dropdown('benefits',$benefits,set_value('benefits'),
$attributes);?>
<span class="text-danger small"><?php echo form_error('benefits'); ?></span>
</div>
<div class="col-md-6">
<?php $attributes = 'class = "form-control" id = "hours"';
echo form_dropdown('hours',$hours,set_value('hours'),
$attributes);?>
<span class="text-danger small"><?php echo form_error('hours'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<input type="text" id="area" name="area" class="form-control" value="" required="required" pattern="" title="" placeholder="Physical area">
</div>
<div class="col-md-6">
<?php $attributes = 'class = "form-control" id = "location"';
echo form_dropdown('location',$location,set_value('location'),
$attributes);?>
<span class="text-danger small"><?php echo form_error('location'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-7">
<div class="input-group">
<span class="input-group-addon">
<i class="icon md-calendar" aria-hidden="true"></i>
</span>
<input type="text" id="dateline" name="dateline" class="form-control" data-plugin="datepicker" data-multidate="true">
<span class="text-danger small"><?php echo form_error('dateline'); ?></span>
</div>
</div>
<div class="col-md-5 pull-right">
<div class="input-group">
<input type="text" id="vacancies" name="vacancies" class="form-control" data-plugin="asSpinner" value="0" />
<span class="text-danger small"><?php echo form_error('vacancies'); ?></span>
</div>
</div>
</div>
</div>
<div class="form-group">
</div>
<div id="alert-msg"></div>
<!-- </form> -->
</div>
<div class="modal-footer">
<input type="reset" class="btn btn-danger" data-dismiss="modal" value="Cancel" />
<input type="submit" name="submit" id="submit" class="btn btn-primary" value="Publish" />
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
&#13;
控制器
public function profilesearch(){
$title['title'] = 'Profile Search | InternCube';
// $this->load->library('pagination');
$this->load->model('Company_model');
$this->load->library('form_validation');
$data['primary'] = $this->Company_model->get_primary();
$data['secondary'] = $this->Company_model->get_secondary();
$data['benefits'] = $this->Company_model->get_benefit();
$data['hours'] = $this->Company_model->get_hours();
$data['location'] = $this->Company_model->get_location();
$data['myprofilesearch'] = $this->Company_model->get_profilesearch();
$this->form_validation->set_rules('tittle', 'Title of the internship', 'trim|required');
$this->form_validation->set_rules('pcategory', 'Primary Category', 'callback_combo_check');
$this->form_validation->set_rules('scategory', 'Secondary Category', 'callback_combo_check');
$this->form_validation->set_rules('sdescription', 'Shot Description of the position', 'trim|required|xss_clean');
$this->form_validation->set_rules('description', 'Description of the position', 'trim|required');
$this->form_validation->set_rules('benefits', 'Benefits', 'callback_combo_check');
$this->form_validation->set_rules('hours', 'Hours', 'callback_combo_check');
$this->form_validation->set_rules('area', 'Physical location', 'trim|required|callback _alpha_only_space');
$this->form_validation->set_rules('location', 'Location', 'callback_combo_check');
$this->form_validation->set_rules('vacancies', 'No of Vacancies', 'trim|required|numeric');
$this->form_validation->set_rules('dateline', 'Deadline', 'required');
if ($this->form_validation->run() == FALSE) {
//fail validation
$this->load->view('template/header', $title);
$this->load->view('company/profilesearch',$data);
$this->load->view('template/footer');
}
else
{
// pass validation
$data = array(
'tittle' => $this->input->post('tittle'),
'short_description' => $this->input >post('sdescription'),
'description' => $this->input->post('description'),
'primary_id' => $this->input->post('pcategory'),
'secondary_id' => $this->input->post('scategory'),
'benefit_id' => $this->input->post('benefits'),
'hours_id' => $this->input->post('hours'),
'location_id' => $this->input->post('location'),
'deadline' => @date('Y-m-d', @strtotime($this->input->post('dateline'))),
'vacancies' => $this->input->post('vacancies'),
);
// insert the form data into database
$this->db->insert('internship_oppotunity', $data);
// display success message
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Employee deta ils added to Database!!!</div>');
redirect('Company/profilesearch');
}
}