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

Hello,

I've been playing with CRUD lately and I have this kind of problem that I didn't know how to fixed. I'm researching in the web to fixed this. Can someone tell me what is the error with this. I got an error A Database Error Occurred Error Number: 1048

My form:
Code:
<div class="activity-item">
                       <form action="" id="form" enctype="multipart/form-data" class="form-horizontal">
                           <input type="hidden" value="" name="id" />
                           <div class="form-body">
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="agencyname" placeholder="Agency Name" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="category" placeholder="Category" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="address" placeholder="Address" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="acronym" placeholder="Acronym" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                           </div>
                       </form>                        
                       <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
                   </div>
SCript:
Code:
<script type="text/javascript">

function save() {
       $('#btnSave').text('saving...');
       $('#btnSave').attr('disabled', true);
       var url;

       if (save_method == 'add') {
           url = "<?php echo site_url('agency/add')?>";
       } else {
           url = "<?php echo site_url('agency/update')?>";
       }
     
       $.ajax({
           url: url,
           type: "POST",
           data: $('#form').serialize(),
           dataType: "JSON",
           success: function(data) {

               if (data.status) //if success close modal and reload ajax table
               {                    
                   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

           }
       });
   }    

   
</script>

Controller:
Code:
    public function add()
    {
        
        $this->_validate();
        $data = array(
            'name' => $this->input->post('agencyname'),
            'category' => $this->input->post('category'),
            'address' => $this->input->post('address'),
            'acronym' => $this->input->post('acronym'),

        );
        $insert = $this->agency->save_agency($data);
        echo json_encode(array("status" => TRUE));
    }
Model:
Code:
public function save_agency($data)
   {
       $this->db->insert($this->agencyTbl, $data);
       return $this->db->insert_id();
   }


Attached Files Thumbnail(s)
   
Reply
#2

(This post was last modified: 07-14-2017, 12:34 AM by Martin7483.)

Are you serious?

The error message says exactly what the problem is.
You are trying to insert an empty row. All the passed values are NULL

So the problem could be located somewhere around the AJAX call
Reply
#3

(This post was last modified: 07-14-2017, 01:02 AM by Junie.)

(07-14-2017, 12:29 AM)Martin7483 Wrote: Are you serious?

The error message says exactly what the problem is.
You are trying to insert an empty row. All the passed values are NULL

So the problem could be located somewhere around the AJAX call

I'm tracing everything or it's just I miss something. I tried using MODAL form it successfully inserted the values. But when I tried a simple form I got that error. I check the Database but its not the problem. I'm pointing the problem when the TIME i clicked the button calling the save function.

In my own understanding the URL is correct. calling on the function.
Reply
#4

Your ajax save function is wrong for the type of form.

Look at this
A good decision is based on knowledge and not on numbers. - Plato

Reply
#5

I think this issue is not on CI itself. This will happen if your form was placed inside of the another form.
[Just a programmer] Cool [/Just a programmer]
Reply
#6

(07-13-2017, 10:44 PM)Junie Wrote: Hello,

I've been playing with CRUD lately and I have this kind of problem that I didn't know how to fixed. I'm researching in the web to fixed this. Can someone tell me what is the error with this. I got an error A Database Error Occurred Error Number: 1048

My form:
Code:
<div class="activity-item">
                       <form action="" id="form" enctype="multipart/form-data" class="form-horizontal">
                           <input type="hidden" value="" name="id" />
                           <div class="form-body">
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="agencyname" placeholder="Agency Name" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="category" placeholder="Category" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="address" placeholder="Address" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="acronym" placeholder="Acronym" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                           </div>
                       </form>                        
                       <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
                   </div>
SCript:
Code:
<script type="text/javascript">

function save() {
       $('#btnSave').text('saving...');
       $('#btnSave').attr('disabled', true);
       var url;

       if (save_method == 'add') {
           url = "<?php echo site_url('agency/add')?>";
       } else {
           url = "<?php echo site_url('agency/update')?>";
       }
     
       $.ajax({
           url: url,
           type: "POST",
           data: $('#form').serialize(),
           dataType: "JSON",
           success: function(data) {

               if (data.status) //if success close modal and reload ajax table
               {                    
                   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

           }
       });
   }    

   
</script>

Controller:
Code:
public function add()
{

$this->_validate();
$data = array(
'name' => $this->input->post('agencyname'),
'category' => $this->input->post('category'),
'address' => $this->input->post('address'),
'acronym' => $this->input->post('acronym'),

);
$insert = $this->agency->save_agency($data);
echo json_encode(array("status" => TRUE));
}
Model:
Code:
public function save_agency($data)
   {
       $this->db->insert($this->agencyTbl, $data);
       return $this->db->insert_id();
   }

1. show us the full html code of the page.

2. console.log($('#form').serialize()) and console.log($('#form')) before $.ajax({

and print the value in this board
Reply
#7

(This post was last modified: 07-16-2017, 08:44 PM by Junie.)

(07-14-2017, 11:14 PM)Paradinight Wrote:
(07-13-2017, 10:44 PM)Junie Wrote: Hello,

I've been playing with CRUD lately and I have this kind of problem that I didn't know how to fixed. I'm researching in the web to fixed this. Can someone tell me what is the error with this. I got an error A Database Error Occurred Error Number: 1048

My form:
Code:
<div class="activity-item">
                       <form action="" id="form" enctype="multipart/form-data" class="form-horizontal">
                           <input type="hidden" value="" name="id" />
                           <div class="form-body">
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="agencyname" placeholder="Agency Name" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="category" placeholder="Category" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="address" placeholder="Address" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                               <div class="form-group">
                                   <div class="col-md-9">
                                       <input name="acronym" placeholder="Acronym" class="form-control" type="text">
                                       <span class="help-block"></span>
                                   </div>
                               </div>
                           </div>
                       </form>                        
                       <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
                   </div>
SCript:
Code:
<script type="text/javascript">

function save() {
       $('#btnSave').text('saving...');
       $('#btnSave').attr('disabled', true);
       var url;

       if (save_method == 'add') {
           url = "<?php echo site_url('agency/add')?>";
       } else {
           url = "<?php echo site_url('agency/update')?>";
       }
     
       $.ajax({
           url: url,
           type: "POST",
           data: $('#form').serialize(),
           dataType: "JSON",
           success: function(data) {

               if (data.status) //if success close modal and reload ajax table
               {                    
                   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

           }
       });
   }    

   
</script>

Controller:
Code:
public function add()
{

$this->_validate();
$data = array(
'name' => $this->input->post('agencyname'),
'category' => $this->input->post('category'),
'address' => $this->input->post('address'),
'acronym' => $this->input->post('acronym'),

);
$insert = $this->agency->save_agency($data);
echo json_encode(array("status" => TRUE));
}
Model:
Code:
public function save_agency($data)
   {
       $this->db->insert($this->agencyTbl, $data);
       return $this->db->insert_id();
   }

1. show us the full html code of the page.

2. console.log($('#form').serialize()) and console.log($('#form')) before $.ajax({

and print the value in this board

Sir,

I've changed a little bit of the code. I'm fixing the problem. Here's the full html code:

Code:
<section id="content">
 <div class="container">
   <div class="row">
     <div class="col-md-12">
       <div class="heading">
         <div class="section-title">Add <span>Agency</span></div>
         <hr>
       </div>
       <div class="col-md-4 col-sm-6 col-xs-12">
         <div class="activity-item">
           <form action="#" id="agencyForm" class="form-horizontal>                        
                           <input type=" hidden " name="agency_id " value=" " id="agency_id ">                            
                           <div class="form-group ">
                               <div class="controls ">
                                   <input type="text " name="input_name " class="form-control "  placeholder="Agency name ">
                                   <span class="help-block "></span>
                               </div>
                           </div>

                           <div class="form-group ">
                               <div class="controls ">
                                   <input type="text " name="input_category " class="form-control "  placeholder="Category ">
                                   <span class="help-block "></span>
                               </div>
                           </div>

                           <div class="form-group ">
                               <div class="controls ">
                                   <input type="text " name="input_address " class="form-control " placeholder="Address ">
                                   <span class="help-block "></span>
                               </div>
                           </div>

                           <div class="form-group ">
                               <div class="controls ">
                                   <input type="text " name="input_acronym " class="form-control "  placeholder="Acronym ">
                                   <span class="help-block "></span>
                               </div>
                           </div>

                       </form>
                       <!-- <input type="submit " name="regisSubmit " class="btn-primary " value="Submit " /> -->
                       <button type="submit " value="submit " onclick="save() " class="btn btn-primary ">Save</button>
                   </div>
               </div>
               <div class="col-md-8 col-sm-6 col-xs-12 ">
                   <div class="activity-item ">
                       <div class="table-responsive ">
                           <table class="table table-striped table-hover table-responsive table-condensed " id="agencyTbl ">
                               <thead>
                                   <tr>
                                       <th>Name</th>
                                       <th>Category</th>
                                       <th>Address</th>
                                       <th>Acronym</th>
                                   </tr>
                               </thead>
                               <tbody id="showagency ">
                               </tbody>
                           </table>
                       </div>
                   </div>
               </div>
           </div>
       </div>
   </section>
   <div class="clearfix "></div>
script:
Code:
< script type = "text/javascript" >
 var save_method; //for save method string

function save() {
 var url;
 if (save_method == 'add') {
   url = "<?php echo site_url('agency/add_agency')?>";
 } else {
   url = "<?php echo site_url('agency/update_update')?>";
 }

 // ajax adding data to database
 var form = new FormData($("#agencyForm")[0]);
 $.ajax({
   url: url,
   type: "POST",
   data: $('#agencyForm').serialize(),
   dataType: "JSON",
   success: function(data) {
     toastr.info('ajax success');
   },
   error: function(jqXHR, textStatus, errorThrown) {
     toastr.info('ajax failed');
   }
 });
}

< /script>

Controller: I include the var_dump to check the passed values. It appears ZERO array
Code:
function add_agency() {
 var_dump($this - > input - > post(NULL, TRUE));
 // $this->_validate();
 $data = array(
   'agency_name' => $this - > input - > post('input_name'),
   'category' => $this - > input - > post('input_category'),
   'address' => $this - > input - > post('input_address'),
   'acronym' => $this - > input - > post('input_acronym'),

 );
 $insert = $this - > agency_model - > add($data);
 echo json_encode(array("status" => TRUE));
}
Console log:

Attached Files Thumbnail(s)
   
Reply
#8

(This post was last modified: 07-16-2017, 11:52 PM by Martin7483.)

Are you posting an empty form on purpose?
Because as shown in your posted image, the form elements returned by $('#form').serialize() are all empty

I did spot this in your HTML

Code:
</form>
<!-- <input type="submit " name="regisSubmit " class="btn-primary " value="Submit " /> -->
<button type="submit " value="submit " onclick="save() " class="btn btn-primary ">Save</button>

Your submit button should be placed before the closing form tag.

Also pass the form as an argument to the save function. You will then know for sure that the correct form will be serialized

[code]
<button type="submit " value="submit " onclick="save(this.form)" class="btn btn-primary ">Save</button>
</form>
Reply
#9

(This post was last modified: 07-17-2017, 01:40 AM by Junie.)

(07-16-2017, 11:51 PM)Martin7483 Wrote: Are you posting an empty form on purpose?
Because as shown in your posted image, the form elements returned by $('#form').serialize() are all empty

I did spot this in your HTML

Code:
</form>
<!-- <input type="submit " name="regisSubmit " class="btn-primary " value="Submit " /> -->
<button type="submit " value="submit " onclick="save() " class="btn btn-primary ">Save</button>

Your submit button should be placed before the closing form tag.

Also pass the form as an argument to the save function. You will then know for sure that the correct form will be serialized

[code]
<button type="submit " value="submit " onclick="save(this.form)" class="btn btn-primary ">Save</button>
</form>
Sir,

I already change that mistake ( button below the closing form tag ). I already successfully submitted the form without the Ajax. Now I'm working on Ajax. It takes time for me coz I'm new in web development. Thanks for the TIME Smile

Note: I tried But it's not working. Using the var_dump($this->input->post(NULL, TRUE)); still array is EMPTY
Reply
#10

console.log($('#agencyForm').serialize());
before $.ajax.

The browser has a debug console. Press the F12 on the keyboard and change to console tab and execute the button click.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB