Welcome Guest, Not a member yet? Register   Sign In
Can't read form values
#21

(This post was last modified: 07-18-2017, 07:42 AM by Junie.)

(07-18-2017, 07: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, 07: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

(This post was last modified: 07-18-2017, 07:43 PM by Junie.)

(07-18-2017, 12: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, 09: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

(This post was last modified: 07-19-2017, 01:55 AM by Martin7483.)

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

(07-19-2017, 01: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
#30

(This post was last modified: 07-22-2017, 04:30 AM by Paradinight.)

(07-19-2017, 05:12 AM)Junie Wrote:
(07-19-2017, 01: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.

1. new FormData(form); is the new way, but the old way do it too (.serialize())
2. the ajax method does not know about multipart/form-data. It does not care about it.

You have only one problem. you does not know how to debug you code Smile
Use the developer console. Every browser has a developer console.

if you send the ajax request, check the developer console. In the network tab you see an entry. Make a screenshot from it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB