Jquery AJAX client side controller design
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I'm working on the client side of a webapp. I've already posted the PHP logics here on codereview and your suggestions have helped me to improve a little bit the code. I'm using Jquery
to manage all the UI/UX and AJAX interactions of the app. I will appreciate musch some tips on how to make this code more efficient. In the code I'm posting, I've defined some functions that manage various aspects. It can appear that I've doubled them, but since I have a medium knowledge of JS and Jquery, for now it's the best approach that I'm able to apply. On this code there is just one modification that I will apply in future and it's about the .serialize()
method. I know it can be useful but with this approach I need to modify all the PHP controller logics, and for now I want to finish first all the necessary logics and client JS code. Also if is needed, please help me to improve the security of the code.
JS
$(document).ready(function()
/* */
displayProductsInterface();
displayCustomerInterface();
diaplaySuppliersInterface();
/* */
insertArticle();
selectArticle();
editArticle();
deleteArticle();
/* */
insertSupplier();
selectSupplier();
editSupplier();
deleteSupplier();
);
function displayCustomerInterface()
$('#manage-customers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'customers.html',
success: function(interface)
$('#products,#suppliers').empty();
$('#customers').html(interface);
);
);
function displayProductsInterface()
$('#manage-products').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'products.php',
success: function(interface)
$('#customers,#suppliers').empty();
$('#products').html(interface);
loadProductsList();
);
);
function diaplaySuppliersInterface()
$('#manage-suppliers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'suppliers.php',
cache: false,
success: function(interface)
$('#products,#customers').empty();
$('#suppliers').html(interface);
loadSuppliersList();
);
);
function loadProductsList()
$.ajax(
type: 'GET',
url: 'do.php?action=productsList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.brand +'</td><td>'+ item.type +'</td><td>'+ item.pieces +'</td><td>'+ item.price +'</td><td>'+ item.tax +'</td><td>'+ item.supplier +'</td><td>'+ item.shelf +'</td><td>'+ item.note +'</td><td><button type="button" class="btn-link edit" value="?action=selectArticle&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#productsTable tbody').append(table);
);
);
function loadSuppliersList()
$.ajax(
type: 'GET',
url: 'do.php?action=suppliersList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.taxid +'</td><td>'+ item.phone +'</td><td>'+ item.fax +'</td><td>'+ item.email +'</td><td><button type="button" class="btn-link edit" value="?action=selectSupplier&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#suppliersTable tbody').append(table);
);
);
function insertArticle()
$(document).on('submit','#productsForm',function(e)
e.preventDefault();
var action = 'insertArticle';
var code = $('#productCode').val();
var pieces = $('#productPieces').val();
var brand = $('#productBrand').val();
var name = $('#productName').val();
var type = $('#productType').val();
var supplier = $('#productSupplier').val();
var shelf = $('#productShelf').val();
var price = $('#productPrice').val();
var taxRate = $('#productTaxRate').val();
var note = $('#productNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function insertSupplier()
$(document).on('submit','#supplierForm',function(e)
e.preventDefault();
var action = 'insertSupplier';
var code = $('#supplierCode').val();
var name = $('#supplierName').val();
var taxid = $('#supplierTaxNumber').val();
var phone = $('#supplierPhone').val();
var fax = $('#supplierFax').val();
var email = $('#supplierEmail').val();
var address = $('#supplierAddress').val();
var cap = $('#supplierCap').val();
var city = $('#supplierCity').val();
var province = $('#supplierProvince').val();
var note = $('#supplierNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function selectArticle()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/productsForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type: 'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
console.log(data);
$('.article-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editPieces').attr('value',data.pieces);
$('#editBrand').attr('value',data.brand);
$('#editName').attr('value',data.name);
$('#editType').attr('value',data.type);
$('#editSupplier').attr('value',data.supplier);
$('#editShelf').attr('value',data.shelf);
$('#editPrice').attr('value',data.price);
$('#editTaxRate').attr('value',data.tax);
$('#editNote').attr('value',data.note);
);
);
);
function selectSupplier()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/suppliersForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type:'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
$('.supplier-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editName').attr('value',data.name);
$('#editTaxNumber').attr('value',data.taxid);
$('#editPhone').attr('value',data.phone);
$('#editFax').attr('value',data.fax);
$('#editEmail').attr('value',data.email);
$('#editAddress').attr('value',data.address);
$('#editCity').attr('value',data.city);
$('#editCap').attr('value',data.cap);
$('#editProvince').attr('value',data.province);
$('#editNote').attr('value',data.note);
);
);
);
function editArticle()
$(document).on('submit','#editProductForm',function(e)
e.preventDefault();
var action = 'editArticle';
var id = $('.article-id').val();
var code = $('#editCode').val();
var pieces = $('#editPieces').val();
var brand = $('#editBrand').val();
var name = $('#editName').val();
var type = $('#editType').val();
var supplier = $('#editSupplier').val();
var shelf = $('#editShelf').val();
var price = $('#editPrice').val();
var taxRate = $('#editTaxRate').val();
var note = $('#editNote').val();
$.ajax(
type: 'POST',
url: 'do.php',
data:action:action,id:id,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function editSupplier()
$(document).on('submit','#editSupplierForm',function(e)
e.preventDefault();
var action = 'editSupplier';
var id = $('.supplier-id').val();
var code = $('#editCode').val();
var name = $('#editName').val();
var taxid = $('#editTaxNumber').val();
var phone = $('#editPhone').val();
var fax = $('#editFax').val();
var email = $('#editEmail').val();
var address = $('#editAddress').val();
var cap = $('#editCap').val();
var city = $('#editCity').val();
var province = $('#editProvince').val();
var note = $('#editNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,id:id,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function deleteArticle()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteArticle';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('fast',function()
loadProductsList();
);
);
);
function deleteSupplier()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteSupplier';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
javascript jquery html ajax
add a comment |Â
up vote
0
down vote
favorite
I'm working on the client side of a webapp. I've already posted the PHP logics here on codereview and your suggestions have helped me to improve a little bit the code. I'm using Jquery
to manage all the UI/UX and AJAX interactions of the app. I will appreciate musch some tips on how to make this code more efficient. In the code I'm posting, I've defined some functions that manage various aspects. It can appear that I've doubled them, but since I have a medium knowledge of JS and Jquery, for now it's the best approach that I'm able to apply. On this code there is just one modification that I will apply in future and it's about the .serialize()
method. I know it can be useful but with this approach I need to modify all the PHP controller logics, and for now I want to finish first all the necessary logics and client JS code. Also if is needed, please help me to improve the security of the code.
JS
$(document).ready(function()
/* */
displayProductsInterface();
displayCustomerInterface();
diaplaySuppliersInterface();
/* */
insertArticle();
selectArticle();
editArticle();
deleteArticle();
/* */
insertSupplier();
selectSupplier();
editSupplier();
deleteSupplier();
);
function displayCustomerInterface()
$('#manage-customers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'customers.html',
success: function(interface)
$('#products,#suppliers').empty();
$('#customers').html(interface);
);
);
function displayProductsInterface()
$('#manage-products').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'products.php',
success: function(interface)
$('#customers,#suppliers').empty();
$('#products').html(interface);
loadProductsList();
);
);
function diaplaySuppliersInterface()
$('#manage-suppliers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'suppliers.php',
cache: false,
success: function(interface)
$('#products,#customers').empty();
$('#suppliers').html(interface);
loadSuppliersList();
);
);
function loadProductsList()
$.ajax(
type: 'GET',
url: 'do.php?action=productsList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.brand +'</td><td>'+ item.type +'</td><td>'+ item.pieces +'</td><td>'+ item.price +'</td><td>'+ item.tax +'</td><td>'+ item.supplier +'</td><td>'+ item.shelf +'</td><td>'+ item.note +'</td><td><button type="button" class="btn-link edit" value="?action=selectArticle&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#productsTable tbody').append(table);
);
);
function loadSuppliersList()
$.ajax(
type: 'GET',
url: 'do.php?action=suppliersList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.taxid +'</td><td>'+ item.phone +'</td><td>'+ item.fax +'</td><td>'+ item.email +'</td><td><button type="button" class="btn-link edit" value="?action=selectSupplier&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#suppliersTable tbody').append(table);
);
);
function insertArticle()
$(document).on('submit','#productsForm',function(e)
e.preventDefault();
var action = 'insertArticle';
var code = $('#productCode').val();
var pieces = $('#productPieces').val();
var brand = $('#productBrand').val();
var name = $('#productName').val();
var type = $('#productType').val();
var supplier = $('#productSupplier').val();
var shelf = $('#productShelf').val();
var price = $('#productPrice').val();
var taxRate = $('#productTaxRate').val();
var note = $('#productNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function insertSupplier()
$(document).on('submit','#supplierForm',function(e)
e.preventDefault();
var action = 'insertSupplier';
var code = $('#supplierCode').val();
var name = $('#supplierName').val();
var taxid = $('#supplierTaxNumber').val();
var phone = $('#supplierPhone').val();
var fax = $('#supplierFax').val();
var email = $('#supplierEmail').val();
var address = $('#supplierAddress').val();
var cap = $('#supplierCap').val();
var city = $('#supplierCity').val();
var province = $('#supplierProvince').val();
var note = $('#supplierNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function selectArticle()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/productsForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type: 'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
console.log(data);
$('.article-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editPieces').attr('value',data.pieces);
$('#editBrand').attr('value',data.brand);
$('#editName').attr('value',data.name);
$('#editType').attr('value',data.type);
$('#editSupplier').attr('value',data.supplier);
$('#editShelf').attr('value',data.shelf);
$('#editPrice').attr('value',data.price);
$('#editTaxRate').attr('value',data.tax);
$('#editNote').attr('value',data.note);
);
);
);
function selectSupplier()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/suppliersForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type:'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
$('.supplier-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editName').attr('value',data.name);
$('#editTaxNumber').attr('value',data.taxid);
$('#editPhone').attr('value',data.phone);
$('#editFax').attr('value',data.fax);
$('#editEmail').attr('value',data.email);
$('#editAddress').attr('value',data.address);
$('#editCity').attr('value',data.city);
$('#editCap').attr('value',data.cap);
$('#editProvince').attr('value',data.province);
$('#editNote').attr('value',data.note);
);
);
);
function editArticle()
$(document).on('submit','#editProductForm',function(e)
e.preventDefault();
var action = 'editArticle';
var id = $('.article-id').val();
var code = $('#editCode').val();
var pieces = $('#editPieces').val();
var brand = $('#editBrand').val();
var name = $('#editName').val();
var type = $('#editType').val();
var supplier = $('#editSupplier').val();
var shelf = $('#editShelf').val();
var price = $('#editPrice').val();
var taxRate = $('#editTaxRate').val();
var note = $('#editNote').val();
$.ajax(
type: 'POST',
url: 'do.php',
data:action:action,id:id,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function editSupplier()
$(document).on('submit','#editSupplierForm',function(e)
e.preventDefault();
var action = 'editSupplier';
var id = $('.supplier-id').val();
var code = $('#editCode').val();
var name = $('#editName').val();
var taxid = $('#editTaxNumber').val();
var phone = $('#editPhone').val();
var fax = $('#editFax').val();
var email = $('#editEmail').val();
var address = $('#editAddress').val();
var cap = $('#editCap').val();
var city = $('#editCity').val();
var province = $('#editProvince').val();
var note = $('#editNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,id:id,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function deleteArticle()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteArticle';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('fast',function()
loadProductsList();
);
);
);
function deleteSupplier()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteSupplier';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
javascript jquery html ajax
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on the client side of a webapp. I've already posted the PHP logics here on codereview and your suggestions have helped me to improve a little bit the code. I'm using Jquery
to manage all the UI/UX and AJAX interactions of the app. I will appreciate musch some tips on how to make this code more efficient. In the code I'm posting, I've defined some functions that manage various aspects. It can appear that I've doubled them, but since I have a medium knowledge of JS and Jquery, for now it's the best approach that I'm able to apply. On this code there is just one modification that I will apply in future and it's about the .serialize()
method. I know it can be useful but with this approach I need to modify all the PHP controller logics, and for now I want to finish first all the necessary logics and client JS code. Also if is needed, please help me to improve the security of the code.
JS
$(document).ready(function()
/* */
displayProductsInterface();
displayCustomerInterface();
diaplaySuppliersInterface();
/* */
insertArticle();
selectArticle();
editArticle();
deleteArticle();
/* */
insertSupplier();
selectSupplier();
editSupplier();
deleteSupplier();
);
function displayCustomerInterface()
$('#manage-customers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'customers.html',
success: function(interface)
$('#products,#suppliers').empty();
$('#customers').html(interface);
);
);
function displayProductsInterface()
$('#manage-products').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'products.php',
success: function(interface)
$('#customers,#suppliers').empty();
$('#products').html(interface);
loadProductsList();
);
);
function diaplaySuppliersInterface()
$('#manage-suppliers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'suppliers.php',
cache: false,
success: function(interface)
$('#products,#customers').empty();
$('#suppliers').html(interface);
loadSuppliersList();
);
);
function loadProductsList()
$.ajax(
type: 'GET',
url: 'do.php?action=productsList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.brand +'</td><td>'+ item.type +'</td><td>'+ item.pieces +'</td><td>'+ item.price +'</td><td>'+ item.tax +'</td><td>'+ item.supplier +'</td><td>'+ item.shelf +'</td><td>'+ item.note +'</td><td><button type="button" class="btn-link edit" value="?action=selectArticle&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#productsTable tbody').append(table);
);
);
function loadSuppliersList()
$.ajax(
type: 'GET',
url: 'do.php?action=suppliersList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.taxid +'</td><td>'+ item.phone +'</td><td>'+ item.fax +'</td><td>'+ item.email +'</td><td><button type="button" class="btn-link edit" value="?action=selectSupplier&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#suppliersTable tbody').append(table);
);
);
function insertArticle()
$(document).on('submit','#productsForm',function(e)
e.preventDefault();
var action = 'insertArticle';
var code = $('#productCode').val();
var pieces = $('#productPieces').val();
var brand = $('#productBrand').val();
var name = $('#productName').val();
var type = $('#productType').val();
var supplier = $('#productSupplier').val();
var shelf = $('#productShelf').val();
var price = $('#productPrice').val();
var taxRate = $('#productTaxRate').val();
var note = $('#productNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function insertSupplier()
$(document).on('submit','#supplierForm',function(e)
e.preventDefault();
var action = 'insertSupplier';
var code = $('#supplierCode').val();
var name = $('#supplierName').val();
var taxid = $('#supplierTaxNumber').val();
var phone = $('#supplierPhone').val();
var fax = $('#supplierFax').val();
var email = $('#supplierEmail').val();
var address = $('#supplierAddress').val();
var cap = $('#supplierCap').val();
var city = $('#supplierCity').val();
var province = $('#supplierProvince').val();
var note = $('#supplierNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function selectArticle()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/productsForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type: 'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
console.log(data);
$('.article-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editPieces').attr('value',data.pieces);
$('#editBrand').attr('value',data.brand);
$('#editName').attr('value',data.name);
$('#editType').attr('value',data.type);
$('#editSupplier').attr('value',data.supplier);
$('#editShelf').attr('value',data.shelf);
$('#editPrice').attr('value',data.price);
$('#editTaxRate').attr('value',data.tax);
$('#editNote').attr('value',data.note);
);
);
);
function selectSupplier()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/suppliersForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type:'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
$('.supplier-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editName').attr('value',data.name);
$('#editTaxNumber').attr('value',data.taxid);
$('#editPhone').attr('value',data.phone);
$('#editFax').attr('value',data.fax);
$('#editEmail').attr('value',data.email);
$('#editAddress').attr('value',data.address);
$('#editCity').attr('value',data.city);
$('#editCap').attr('value',data.cap);
$('#editProvince').attr('value',data.province);
$('#editNote').attr('value',data.note);
);
);
);
function editArticle()
$(document).on('submit','#editProductForm',function(e)
e.preventDefault();
var action = 'editArticle';
var id = $('.article-id').val();
var code = $('#editCode').val();
var pieces = $('#editPieces').val();
var brand = $('#editBrand').val();
var name = $('#editName').val();
var type = $('#editType').val();
var supplier = $('#editSupplier').val();
var shelf = $('#editShelf').val();
var price = $('#editPrice').val();
var taxRate = $('#editTaxRate').val();
var note = $('#editNote').val();
$.ajax(
type: 'POST',
url: 'do.php',
data:action:action,id:id,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function editSupplier()
$(document).on('submit','#editSupplierForm',function(e)
e.preventDefault();
var action = 'editSupplier';
var id = $('.supplier-id').val();
var code = $('#editCode').val();
var name = $('#editName').val();
var taxid = $('#editTaxNumber').val();
var phone = $('#editPhone').val();
var fax = $('#editFax').val();
var email = $('#editEmail').val();
var address = $('#editAddress').val();
var cap = $('#editCap').val();
var city = $('#editCity').val();
var province = $('#editProvince').val();
var note = $('#editNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,id:id,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function deleteArticle()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteArticle';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('fast',function()
loadProductsList();
);
);
);
function deleteSupplier()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteSupplier';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
javascript jquery html ajax
I'm working on the client side of a webapp. I've already posted the PHP logics here on codereview and your suggestions have helped me to improve a little bit the code. I'm using Jquery
to manage all the UI/UX and AJAX interactions of the app. I will appreciate musch some tips on how to make this code more efficient. In the code I'm posting, I've defined some functions that manage various aspects. It can appear that I've doubled them, but since I have a medium knowledge of JS and Jquery, for now it's the best approach that I'm able to apply. On this code there is just one modification that I will apply in future and it's about the .serialize()
method. I know it can be useful but with this approach I need to modify all the PHP controller logics, and for now I want to finish first all the necessary logics and client JS code. Also if is needed, please help me to improve the security of the code.
JS
$(document).ready(function()
/* */
displayProductsInterface();
displayCustomerInterface();
diaplaySuppliersInterface();
/* */
insertArticle();
selectArticle();
editArticle();
deleteArticle();
/* */
insertSupplier();
selectSupplier();
editSupplier();
deleteSupplier();
);
function displayCustomerInterface()
$('#manage-customers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'customers.html',
success: function(interface)
$('#products,#suppliers').empty();
$('#customers').html(interface);
);
);
function displayProductsInterface()
$('#manage-products').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'products.php',
success: function(interface)
$('#customers,#suppliers').empty();
$('#products').html(interface);
loadProductsList();
);
);
function diaplaySuppliersInterface()
$('#manage-suppliers').on('click',function(e)
e.preventDefault();
$.ajax(
type:'GET',
url: 'suppliers.php',
cache: false,
success: function(interface)
$('#products,#customers').empty();
$('#suppliers').html(interface);
loadSuppliersList();
);
);
function loadProductsList()
$.ajax(
type: 'GET',
url: 'do.php?action=productsList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.brand +'</td><td>'+ item.type +'</td><td>'+ item.pieces +'</td><td>'+ item.price +'</td><td>'+ item.tax +'</td><td>'+ item.supplier +'</td><td>'+ item.shelf +'</td><td>'+ item.note +'</td><td><button type="button" class="btn-link edit" value="?action=selectArticle&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#productsTable tbody').append(table);
);
);
function loadSuppliersList()
$.ajax(
type: 'GET',
url: 'do.php?action=suppliersList',
cache: false,
success: function(response)
var table = '';
$.each(JSON.parse(response), function(i, item)
table = '<tr><td>'+ item.code +'</td><td>'+ item.name +'</td><td>'+ item.taxid +'</td><td>'+ item.phone +'</td><td>'+ item.fax +'</td><td>'+ item.email +'</td><td><button type="button" class="btn-link edit" value="?action=selectSupplier&id='+ item.id +'" data-toggle="modal" data-target="#editForms">Modifica</button> <button class="btn-link delete" value="'+ item.id +'">Elimina</button></td>';
$('#suppliersTable tbody').append(table);
);
);
function insertArticle()
$(document).on('submit','#productsForm',function(e)
e.preventDefault();
var action = 'insertArticle';
var code = $('#productCode').val();
var pieces = $('#productPieces').val();
var brand = $('#productBrand').val();
var name = $('#productName').val();
var type = $('#productType').val();
var supplier = $('#productSupplier').val();
var shelf = $('#productShelf').val();
var price = $('#productPrice').val();
var taxRate = $('#productTaxRate').val();
var note = $('#productNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function insertSupplier()
$(document).on('submit','#supplierForm',function(e)
e.preventDefault();
var action = 'insertSupplier';
var code = $('#supplierCode').val();
var name = $('#supplierName').val();
var taxid = $('#supplierTaxNumber').val();
var phone = $('#supplierPhone').val();
var fax = $('#supplierFax').val();
var email = $('#supplierEmail').val();
var address = $('#supplierAddress').val();
var cap = $('#supplierCap').val();
var city = $('#supplierCity').val();
var province = $('#supplierProvince').val();
var note = $('#supplierNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function selectArticle()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/productsForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type: 'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
console.log(data);
$('.article-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editPieces').attr('value',data.pieces);
$('#editBrand').attr('value',data.brand);
$('#editName').attr('value',data.name);
$('#editType').attr('value',data.type);
$('#editSupplier').attr('value',data.supplier);
$('#editShelf').attr('value',data.shelf);
$('#editPrice').attr('value',data.price);
$('#editTaxRate').attr('value',data.tax);
$('#editNote').attr('value',data.note);
);
);
);
function selectSupplier()
$(document).on('click','button.btn-link.edit',function(e)
e.preventDefault();
var href = $(this).val();
$('.modal-body').empty();
$.ajax(
type: 'GET',
url: 'view/suppliersForm.html',
cache: false,
success: function(interface)
$('.modal-body').html(interface);
$.ajax(
type:'GET',
url: 'do.php'+href,
cache: false,
success: function(response)
var data = JSON.parse(response);
$('.supplier-id').attr('value',data.id);
$('#editCode').attr('value',data.code);
$('#editName').attr('value',data.name);
$('#editTaxNumber').attr('value',data.taxid);
$('#editPhone').attr('value',data.phone);
$('#editFax').attr('value',data.fax);
$('#editEmail').attr('value',data.email);
$('#editAddress').attr('value',data.address);
$('#editCity').attr('value',data.city);
$('#editCap').attr('value',data.cap);
$('#editProvince').attr('value',data.province);
$('#editNote').attr('value',data.note);
);
);
);
function editArticle()
$(document).on('submit','#editProductForm',function(e)
e.preventDefault();
var action = 'editArticle';
var id = $('.article-id').val();
var code = $('#editCode').val();
var pieces = $('#editPieces').val();
var brand = $('#editBrand').val();
var name = $('#editName').val();
var type = $('#editType').val();
var supplier = $('#editSupplier').val();
var shelf = $('#editShelf').val();
var price = $('#editPrice').val();
var taxRate = $('#editTaxRate').val();
var note = $('#editNote').val();
$.ajax(
type: 'POST',
url: 'do.php',
data:action:action,id:id,code:code,pieces:pieces,name:name,brand:brand,type:type,supplier:supplier,shelf:shelf,price:price,tax:taxRate,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadProductsList();
);
);
);
function editSupplier()
$(document).on('submit','#editSupplierForm',function(e)
e.preventDefault();
var action = 'editSupplier';
var id = $('.supplier-id').val();
var code = $('#editCode').val();
var name = $('#editName').val();
var taxid = $('#editTaxNumber').val();
var phone = $('#editPhone').val();
var fax = $('#editFax').val();
var email = $('#editEmail').val();
var address = $('#editAddress').val();
var cap = $('#editCap').val();
var city = $('#editCity').val();
var province = $('#editProvince').val();
var note = $('#editNote').val();
$.ajax(
url: 'do.php',
type: 'POST',
data: action:action,id:id,code:code,name:name,taxid:taxid,phone:phone,fax:fax,email:email,address:address,cap:cap,city:city,province:province,note:note,
cache: false,
success: function(response)
console.log(response);
$('#editForms').modal('hide');
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
function deleteArticle()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteArticle';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#productsTable tbody').fadeOut()
.empty()
.fadeIn('fast',function()
loadProductsList();
);
);
);
function deleteSupplier()
$(document).on('click','button.btn-link.delete',function(e)
e.preventDefault();
var action = 'deleteSupplier';
var id = $(this).val();
$.ajax(
type: 'POST',
data: action:action,id:id,
url: 'do.php',
cache: false,
success: function(response)
$('#suppliersTable tbody').fadeOut()
.empty()
.fadeIn('slow',function()
loadSuppliersList();
);
);
);
javascript jquery html ajax
asked May 23 at 11:05
user9741470
1009
1009
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195015%2fjquery-ajax-client-side-controller-design%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password