• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't read form values

#21
(07-18-2017, 08:27 AM)Martin7483 Wrote: Because your agency_id is empty

Sir,

It's auto incremented. I have the habit to leave as blank when I configure as AI in the DB. I tried to give a value but it doesn't insert its value in the DB.
Reply

#22
So you say the serialize is working as it should.
What does your var_dump on the input->post show?
Reply

#23
(07-18-2017, 08:45 AM)Martin7483 Wrote: So you say the serialize is working as it should.
What does your var_dump on the input->post show?

Code:
array(0) { }
 and.
Code:
Error Number: 1048

Column 'agency_name' cannot be null

INSERT INTO `agency` (`agency_name`, `category`, `address`, `acronym`) VALUES (NULL, NULL, NULL, NULL)

Filename: C:/xampp/htdocs/csc/system/database/DB_driver.php

Line Number: 691
Reply

#24
I give up.

You are doing something which is causing strange behaviour.

Maybe you could attach the files to this post so we could see what you are doing
Reply

#25
(07-18-2017, 01:43 PM)Martin7483 Wrote: I give up.

You are doing something which is causing strange behaviour.

Maybe you could attach the files to this post so we could see what you are doing

Sir,

This is my currently code:

Form:
Code:
<div class="col-md-4 col-sm-6 col-xs-12">
 <div class="activity-item">
   <form id="agencyForm" enctype="multipart/form-data" method="POST" class="form-horizontal">
     <div class="form-body">
       <input type="hidden" value="" name="agency_id" />
       <div class="form-group">
         <div class="col-md-12">
           <input name="agency_name" id="agency_name" placeholder="Agency Name" class="form-control" type="text">
           <?php echo form_error('agency_name','<span class="help-block">','</span>'); ?>
         </div>
       </div>
       <div class="form-group">
         <div class="col-md-12">
           <input name="category" id="category" placeholder="Category" class="form-control" type="text">
           <?php echo form_error('category','<span class="help-block">','</span>'); ?>
         </div>
       </div>
       <div class="form-group">
         <div class="col-md-12">
           <input name="address" id="address" placeholder="Address" class="form-control" type="text">
           <?php echo form_error('address','<span class="help-block">','</span>'); ?>
         </div>
       </div>
       <div class="form-group">
         <div class="col-md-12">
           <input name="acronym" id="acronym" placeholder="Acronym" class="form-control" type="text">
           <?php echo form_error('acronym','<span class="help-block">','</span>'); ?>
         </div>
       </div>
     </div>
     <button type="submit" value="submit " id="btnSave" onclick="save(this.agencyForm); return false" class="btn btn-effect">Save</button>
   </form>

 </div>
 <div class="clear"> </div>
</div>
Partially script:
Code:
function save() {
 $('#btnSave').text('saving...');
 $('#btnSave').attr('disabled', true);
 var url;
 // var form = new FormData($("#agencyForm")[0]);        

 if (save_method == 'add') {
   url = "<?php echo site_url('agency/save_c')?>";
 } else {
   // url = "<?php echo site_url('agency/update')?>";
 }

 console.log($('#agencyForm').serialize());
 // alert($(agencyForm).serialize());
 $.ajax({
   url: url,
   type: "POST",
   data: $('#agencyForm').serialize(),
   dataType: "JSON",
   success: function(data) {

     if (data.status) {
       alert('Successfully added the officer');
       reload_table();

     } else {

     }
     $('#btnSave').text('save'); //change button text
     $('#btnSave').attr('disabled', false); //set button enable


   },
   error: function(jqXHR, textStatus, errorThrown) {
     alert('Error adding / update data');
     $('#btnSave').text('save'); //change button text
     $('#btnSave').attr('disabled', false); //set button enable

   }
 });
}

Controller:
Code:
public function save_c()
    {
        
        var_dump($this->input->post(NULL, TRUE));
        // $this->_validate();
        $data = array(
            'agency_name' => $this->input->post('agency_name'),
            'category' => $this->input->post('category'),
            'address' => $this->input->post('address'),
            'acronym' => $this->input->post('acronym'),
        );
        $insert = $this->agency->save_now($data);
        echo json_encode(array("status" => TRUE));
    }
Model:
Code:
public function save_now($data)
   {
       $this->db->insert($this->table, $data);
       return $this->db->insert_id();
   }

NOTE: It insert data into the database when I change the URL like this:
Code:
$.ajax({
           // url: url,
           url: "<?php echo site_url('agency/save_c')?>",
           type: "POST",
           data: $('#agencyForm').serialize(),            
           dataType: "JSON",
           success: function(data) {

               if (data.status)
               {
                alert('Successfully added the officer');
                reload_table();

            } else {

            }
               $('#btnSave').text('save'); //change button text
               $('#btnSave').attr('disabled', false); //set button enable


           },
           error: function(jqXHR, textStatus, errorThrown) {
               alert('Error adding / update data');
               $('#btnSave').text('save'); //change button text
               $('#btnSave').attr('disabled', false); //set button enable

           }
       });

Thanks for the TimeSmile
Reply

#26
I had a similar problem before.
The problem is the form type "multipart/form-data",  Ajax handle this type of form differently (don't ask me why or how as I don't know).
As you only have text field in your form try to use the default enctype
Or if you need to use multipart/form-data, look at the link in post #4

https://stackoverflow.com/questions/3971...ing-jquery
A good decision is based on knowledge and not on numbers. - Plato

Reply

#27
(07-18-2017, 10:58 PM)salain Wrote: I had a similar problem before.
The problem is the form type "multipart/form-data",  Ajax handle this type of form differently (don't ask me why or how as I don't know).
As you only have text field in your form try to use the default enctype
Or if you need to use multipart/form-data, look at the link in post #4

https://stackoverflow.com/questions/3971...ing-jquery

Sir,

Based on your past problem, so changing the form type will solve the problem or should I have to change my controllers or other parts of the code?
Reply

#28
Remove the enctype="multipart/form-data" attribute from the opening form tag

That attribute is only needed when uploading files.

I missed this up untill now, but that indeed could be the cause.
If you do require uploading files in the future, you will need to do this;

Code:
function save(form) {
   // NO!!! Don't do this!
   var formData = $(form).serialize();

   // YES!!! Do this!
   var formData = new FormData(form);

   $.ajax({
           url: url,
           type: "POST",
           data: formData,
           dataType: "JSON",
           ......
           ......
}
Reply

#29
(Yesterday, 02:49 AM)Martin7483 Wrote: Remove the enctype="multipart/form-data" attribute from the opening form tag

That attribute is only needed when uploading files.

I missed this up untill now, but that indeed could be the cause.
If you do require uploading files in the future, you will need to do this;

Code:
function save(form) {
   // NO!!! Don't do this!
   var formData = $(form).serialize();

   // YES!!! Do this!
   var formData = new FormData(form);

   $.ajax({
           url: url,
           type: "POST",
           data: formData,
           dataType: "JSON",
           ......
           ......
}

Sir, 

But for now, I just need input text and removing the enctype="multipart/form-data" doesn't change anything. My idea of creating a one-page CRUD in codeigniter seems too hard for a beginner. If I can't correct the errors I may use the modal way but as this moment I will stick on one-page crud. 
Thanks for the TIME.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.