**** HTML
<input type="text" class="form-control" name="admin_price" value="30000" data-pid="149">
<input type="text" class="form-control" name="admin_saleoffprice" value="10000" data-pid="149" style="border-color: rgb(217, 83, 79);">
**** JS
$(document).on('keyup', 'input[name="admin_price"], input[name="admin_saleoffprice"]', function () {
const $input = $(this);
const pid = $input.data('pid');
const field = $input.attr('name') === 'admin_price' ? 'price' : 'saleoff_price';
const value = $input.val().replace(/[^\d]/g, ''); // chỉ giữ số
clearTimeout($input.data('timer')); // timer riêng cho từng ô
$input.data('timer', setTimeout(function () {
$.ajax({
url: '/modules.php',
type: 'POST',
dataType: 'html',
data: { name: 'Shoping', op: 'ajax_gia_sp', pid: pid, field: field, value: value },
beforeSend: function () {
$input.css('border-color', '#f0ad4e'); // đang lưu
},
success: function (res) {
console.log(res);
$input.css('border-color', $.trim(res) == 1 ? '#5cb85c' : '#d9534f');
},
error: function (xhr, status, err) {
console.log(xhr.status, err);
console.log(xhr.responseText);
$input.css('border-color', '#d9534f');
}
});
}, 500));
});
/*** FUNCTION ***/
function ajax_gia_sp(){
ini_set('display_errors', 1);
error_reporting(E_ALL);
global $db, $data_cua_ban;
// Danh sách cột được phép sửa (map từ field gửi lên -> tên cột thật)
$allow = array(
'price' => 'price',
'saleoff_price' => 'saleoff_price'
);
$pid = isset($_POST['pid']) ? (int)$_POST['pid'] : 0;
$field = isset($_POST['field']) ? $_POST['field'] : '';
$value = isset($_POST['value']) ? (int)preg_replace('/[^\d]/', '', $_POST['value']) : 0;
if ($pid <= 0 || !isset($allow[$field])) {
echo 0;
exit;
}
$col = $allow[$field];
$sql = "UPDATE `".$data_cua_ban."_shop` SET `".$col."` = '".$value."' WHERE `pid` = '".$pid."'";
echo "POST: "; print_r($_POST); echo "\n";
echo "SQL: ".$sql."\n";
$result = $db->sql_query($sql);
echo "Result: "; var_dump($result); echo "\n";
if (method_exists($db, 'sql_error')) { print_r($db->sql_error()); }
if (method_exists($db, 'sql_affectedrows')) { echo "Affected rows: ".$db->sql_affectedrows()."\n"; }
exit;
}
/*** TỪ 3 CỘT (FEILD) TRƠ LÊN ***/
<input type="text" class="form-control ajax-money" name="admin_price" data-field="price" value="0" data-pid="149">
<input type="text" class="form-control ajax-money" name="admin_saleoffprice" data-field="saleoff_price" value="0" data-pid="149">
<input type="text" class="form-control ajax-money" name="admin_sale" data-field="sale_price" value="0" data-pid="149">
$(document).on('keyup', '.ajax-money', function () {
const $input = $(this);
const pid = $input.data('pid');
const field = $input.data('field');
const value = $input.val().replace(/[^\d]/g, ''); // chỉ giữ số
clearTimeout($input.data('timer')); // timer riêng từng ô
$input.data('timer', setTimeout(function () {
$.ajax({
url: '/modules.php',
type: 'POST',
dataType: 'html',
data: { name: 'Shoping', op: 'ajax_gia_sp', pid: pid, field: field, value: value },
beforeSend: function () {
$input.css('border-color', '#f0ad4e');
}
})
.done(function (res) {
console.log(res);
$input.css('border-color', $.trim(res) == 1 ? '#5cb85c' : '#d9534f');
})
.fail(function (xhr, status, err) {
console.log(xhr.status, err);
console.log(xhr.responseText);
$input.css('border-color', '#d9534f');
});
}, 500));
});
function ajax_update_price(){
global $db, $data_cua_ban;
// field gửi lên => tên cột thật trong bảng
$allow = array(
'price' => 'price',
'saleoffprice' => 'saleoff_price',
'sale' => 'sale_price',
);
$pid = isset($_POST['pid']) ? (int)$_POST['pid'] : 0;
$field = isset($_POST['field']) ? $_POST['field'] : '';
$value = isset($_POST['value']) ? (int)preg_replace('/[^\d]/', '', $_POST['value']) : 0;
if ($pid <= 0 || !isset($allow[$field])) {
echo 0;
exit;
}
$col = $allow[$field];
$result = $db->sql_query("UPDATE `".$data_cua_ban."_shop` SET `".$col."` = '".$value."' WHERE `pid` = '".$pid."'");
echo $result ? 1 : 0;
exit;
}

Không có nhận xét nào:
Đăng nhận xét