Как справиться с возвращенными данными JSON [дублировать]Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как справиться с возвращенными данными JSON [дублировать]

Сообщение Anonymous »

Я создаю редактируемую строку в сценарии HTML, PHP и Java.

Код: Выделить всё






[i][/i]
[i][/i]




< /code>
, и следующее приведено сценарий для обработки сохранения BTN (я включил обновление и удаление): < /p>
$(document).ready(function() {
$('[data-bs-toggle="tooltip"]').tooltip();

$('td[contenteditable="true"]').on('input', function() {
$(this).closest('tr').find('.save-btn').css('opacity', '1').css('cursor', 'pointer');
});

// Save changes
$(document).on("click", ".save-btn", function() {
let row = $(this).closest("tr");
let id = row.data("id");
let roomType = row.find(".room-type").text().trim();
let roomDesc = row.find(".room-desc").text().trim();

console.log("Saving:", roomType, roomDesc);

$.post("rooms.functions.php", {
action: id ? "update" : "add",
id,
roomType,
roomDesc
}, function(response) {
console.log("Raw Response:", response); // Log the raw response

// Only parse the response if it's valid JSON
try {
let res = JSON.parse(response);
alert(res.message);
if (res.status === "success" && !id) {
row.attr("data-id", res.id);
row.find("td:first").text(res.id);
}
} catch (e) {
console.error("JSON Parse Error---> ", e);
alert("Error processing request. Please check the console for details.");
}
});
});

// Delete room type
$(document).on("click", ".delete-btn", function() {
let row = $(this).closest("tr");
let id = row.data("id");

if (confirm("Are you sure you want to delete this room type?")) {
$.post("rooms.functions.php", {
action: "delete",
id
}, function(response) {
console.log("Raw Response (Delete):", response);
try {
let res = JSON.parse(response);
alert(res.message);
if (res.status === "success") {
row.remove();
}
} catch (e) {
console.error("JSON Parse Error:", e);
alert("Error processing request.  Check console for details.");
}
});
}
});

// Add new row
$("#addNew").click(function() {
let newRow = $(`

New



[i][/i]
[i][/i]


`);
$("#roomTypeTable").append(newRow);
});
});
< /code>
Ниже приведен PHP, который управляет CRUD: < /p>


Подробнее здесь: [url]https://stackoverflow.com/questions/79497180/how-to-deal-with-returned-json-data[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»