I've search thru the web about CRUD in PHP jquery Ajax using modal form and i can say all the snippets i find is not accurate and helpful so i decided to share my own snippets to you guys.
First of all download all the components to complete this project. I supposed you guys have the knowledge how to get this component and make it run. Follow the link on the above components and you will be alright.
I will not be discussing here how to install and set up codeigniter. You can follow thisdetailed User Guidefrom codeIgniter.
Lets Start Coding.
File Structure:
Now go to your Htdocs folder and make a folder called assets. Inside this folder we will make 2 new folder and name it js and css. Here we will paste the bootstrap jquery and datatables components.
assets/js folder
assets/css folder
Creating Database:
Open your localhost phpmyadmin and create a database named project.
Code Igniter Database Settings:
I'll make it a little simpler lest skip the migration on CodeIgniter. Let's create a table from your database name project.
in codeigniter create a model.Here we will create function for all the database interaction. I created a model name Project_model below is the code.
Model Project_model.php
Controller MycrudProject.php
HTML Views Folder
Jquery assets/js/project.js
';
}//end for
title.empty();
title.append('Add New Product');
fields.empty();
fields.append(input);
fields.find('div:first-child').children('input').attr('type','hidden');
fields.find('div:first-child').children('label').hide();
fields.find('div:nth-last-child(3)').hide();
fields.find('div:nth-last-child(2)').hide();
fields.find('div:last-child').hide();
//select option
$.ajax({
type:'Post',
"url": 'myCrudproject/jsondata/2',
"dataType": "json",
success:function(data){
var text=fields.find('div:nth-last-child(5) input');
var preselect=$(''), options='';
for(var x=0;x'+data[x].type_name+'';
}
var select= preselect.append(options);
select.prepend('');
text.replaceWith(select);
}
});
//button add
$('#add').on('click',function(){
fields.show();
toggle.val(1);
del.hide();
$('#btn_form').val('New Record');
var table= $('#project');
form.modal('toggle');
$('#ID').val('');
$('#Name').val('');
$('#Price').val('');
$('#type option:selected').html('Select Value');
$('#type option:selected').val('Select');
$('#Stocks').val('');
});//end button add
//edit button
$('#project tbody').on('click', '.edit', function(){
fields.show();
del.hide();
toggle.val(2);
form.modal('toggle');
title.empty();
title.append('Edit Products');
$('#btn_form').val('Update Record');
var data= [];
$(this).closest('tr').each(function (rowIndex) {
var cols = [];
$(this).find('td').each(function (colIndex, c) {
cols.push(c.textContent);
});
data.push(cols);
});
$.each(data, function(k,v){
var pr =v[2].split('$');
$('#ID').val(v[0]);
$('#Name').val(v[1]);
$('#Price').val(pr[1]);
$('#type option:selected').html(v[3]);
$('#type option:selected').val(v[3]);
var st =v[4].split('Pcs');
$('#Stocks').val(st[0]);
});
});
$('#project tbody').on('click', '.delete', function(){
$('#myform').modal('toggle');
title.empty();
title.append('Delete Record');
fields.hide();
toggle.val(3);
del.show();
del.empty();
var id=$(this).closest('tr').find('td:first').text();
$('#btn_form').val('Delete');
del.append('
Are you sure you want to Delete?
');
del.append($(''));
});
});// doc ready
For more details download the Project file on this link
Comment down below your questions and i'll be happy to answer. Happy coding.