Question

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

enter image description here

Serialize values showed in alert enter image description here

Errors

enter image description here

enter image description here

Was it helpful?

Solution 3

The form id was just mispelled lacking 'r'.

OTHER TIPS

I know how that feels. When dealing with AJAX, it's better to add this line into your javascript when debugging

.fail(function(data) {
    console.log(data);
}

Which will make your javascript looks like this:

<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")
    .fail(function(data) {
        console.log(data);
    });
 });
</script>

This way, you will be able to see what the error message in the console when it hits 500 Internal Server Error.

Note: I'm using Google Chrome and it works. You might have to turn on the AJAX request or something (I forgot what it's called)

The sql is incorrect. You can see there is several field in your table tbl_agri_technician. But in the last photo you post which sql insert statement is

INSERT INTO tbl_agri_technician (0) VALUES ('')

Which is totally not match the table. It might be the error of mapping table. I guess there is some error in your setting.

It's better debug from the core_model.php and check the variable "$agri_tech_details".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top