I have been doing this several times and I am sure my code is just the same with the codes that have been working out for me in saving the data into database using $.post approach. However, this code that I have now returns an internal server error and I have been toying around with this for number of hours and it seems there is no problem with the code yet it doesn't work out for me. Can you please point to me the problem with my codes and how can I solve it? Please help. Thanks a lot. Here are my codes.
Form
<form id="agi_tech_form" method="post">
<table class="table table-bordered table-hover">
<tbody>
<tr>
<td><label for="at_fname"><span class="lead at-text">First Name</span></label><input type="text" class="form-control" name="at[fname]" id="at_fname" value="" placeholder="First Name" style= "width: 300px;"/></td>
<td><label for="at_lname"><span class="lead at-text">Last Name</span></label><input type="text" class="form-control" name="at[lname]" id="at_lname" value="" placeholder="Last Name" style= "width: 300px;" /></td>
</tr>
<tr>
<td><label for="at_mname"><span class="lead at-text">Middle Name</span></label><input type="text" class="form-control" name="at[mname]" id="at_mname" value="" placeholder="Middle Name" style= "width: 300px;"/></td>
<td><label for="at_gender"><span class="lead at-text">Gender</span></label><input type="text" class="form-control" name="at[gender]" id="at_gender" value="" placeholder="Gender" style= "width: 300px;" /></td>
</tr>
<tr>
<td><label for="at_bday"><span class="lead at-text">Birth Date</span></label><input type="date" class="form-control" name="at[bday]" id="at_bday" value="" placeholder="Birth date" style= "width: 300px;"/></td>
<td><label for="at_contact"><span class="lead at-text">Cell Phone Number</span></label><input type="text" class="form-control" name="at[contact]" id="at_contact" value="" placeholder="Cell Number" style= "width: 300px;" /></td>
</tr>
<tr>
<td><label for="at_address"><span class="lead at-text">Address</span></label><input type="text" class="form-control" name="at[address]" id="at_address" value="" placeholder="Address" style= "width: 300px;"/></td>
<td><label for="at_email"><span class="lead at-text">Email Address</span></label><input type="email" class="form-control" name="at[email]" id="at_email" value="" placeholder="Email Address" style= "width: 300px;" /></td>
</tr>
<tr>
<td><label for="at_brgy1"><span class="lead at-text">1st Barangay</span></label>
<?php
//$attr = array('class'=>'form-control','id'=>'at_brgy1');
echo form_dropdown('at[brgy1]',$dropdown_brgy1,'','id="brgy_id_1" class="form-control" style="width:300px"'); ?>
</td>
<td><label for="at_brgy2"><span class="lead at-text">2nd Barangay</span></label>
<?php $dropdown_brgy2 = array('0'=>'Select Barangay');
//$attr = array('class'=>'form-control','id'=>'at_brgy1');
echo form_dropdown('at[brgy2]',$dropdown_brgy2,'','id="brgy_id_2" class="form-control" style="width:300px"'); ?>
</td>
</tr>
<tr>
<td><label for="at_brgy3"><span class="lead at-text">3rd Barangay</span></label>
<?php $dropdown_brgy3 = array('0'=>'Select Barangay');
//$attr = array('class'=>'form-control','id'=>'at_brgy1');
echo form_dropdown('at[brgy3]',$dropdown_brgy3,'','id="brgy_id_3" class="form-control" style="width:300px"'); ?>
</td>
</tr>
</tbody>
</table>
</form>
<table class="table table-bordered">
<tr>
<td><button type="button" class="btn btn-success" id="agri-btn">Save Details</button></td>
</tr>
</table>
Javascript:
<script type="text/javascript">
$("#agri-btn").click(function(){
alert($("#agi_tech_form").serialize());
$.post('<?php echo base_url(); ?>admin/save_agri_tech',$("#agri_tech_form").serialize(),function(data){
if(data.notify == "Success"){
console.log(data.notify);
}
},"json");
});
</script>
admin.php
function save_agri_tech(){
$agri_tech_details = $this->input->post('at');
$query = $this->core_model->save_at_details($agri_tech_details);
if( $query ){
$notification = "Success";
} else{
$notification = "Failed";
}
echo json_encode(array('notify'=>$notification));
}
core_model.php
function save_at_details($agri_tech_details){
$this->db->insert('tbl_agri_technician',$agri_tech_details);
return true;
}
tbl_agri_technician structure
Serialize values showed in alert
Errors
The form id was just mispelled lacking 'r'.