-
Junie
Member
-
Posts: 53
Threads: 6
Joined: May 2017
Reputation:
-1
(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?
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
-
Junie
Member
-
Posts: 53
Threads: 6
Joined: May 2017
Reputation:
-1
07-18-2017, 06:55 PM
(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 Time
-
salain
Member
-
Posts: 135
Threads: 2
Joined: Nov 2014
Reputation:
12
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
-
Martin7483
Crossfire CMS
-
Posts: 373
Threads: 14
Joined: Sep 2015
Reputation:
20
07-19-2017, 01:49 AM
(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",
......
......
}
-
Junie
Member
-
Posts: 53
Threads: 6
Joined: May 2017
Reputation:
-1
(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.
-
Paradinight
Senior Member
-
Posts: 445
Threads: 6
Joined: Jun 2015
Reputation:
25
07-22-2017, 04:29 AM
(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
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.
|