[PHP]비동기 CRUD(JS)

김이홍·2024년 1월 19일

PHP

목록 보기
19/30
$(document).on('click', '#btn-add', function (e) {
	var data = $("#user_form").serialize();
	$.ajax({
		data: data,
		type: "post",
		url: "backend/save.php",
		success: function (dataResult) {
			dataResult = JSON.parse(dataResult);
			if (dataResult.statusCode == 200) {
				$('#addEmployeeModal').modal('hide');
				alert('Data added successfully !');
				location.reload();
			},
		error: function (xhr, status, error) {
			alert("An error occurred. HTTP Status: " + xhr.status + ". Status: " + status + ". Error: " + error);
		},
		}
	});
});

$(document).on('click', '.update', function (e) {
	var id = $(this).attr("data-id");
	var name = $(this).attr("data-name");
	var email = $(this).attr("data-email");
	var phone = $(this).attr("data-phone");
	var city = $(this).attr("data-city");
	$('#id_u').val(id);
	$('#name_u').val(name);
	$('#email_u').val(email);
	$('#phone_u').val(phone);
	$('#city_u').val(city);
});

$(document).on('click', '#update', function (e) {
	var data = $("#update_form").serialize();
	$.ajax({
		data: data,
		type: "post",
		url: "backend/save.php",
		success: function (dataResult) {
			ataResult = JSON.parse(dataResult);
			if (dataResult.statusCode == 200) {
				$('#editEmployeeModal').modal('hide');
				alert('Data updated successfully !');
				location.reload();
			},
		error: function (xhr, status, error) {
			alert("An error occurred. HTTP Status: " + xhr.status + ". Status: " + status + ". Error: " + error);
		},
		}
	});
});

$(document).on("click", ".delete", function () {
	var id = $(this).attr("data-id");
	$('#id_d').val(id);
});

$(document).on("click", "#delete", function () {
	$.ajax({
		url: "backend/save.php",
		type: "POST",
		cache: false,
		data: {
			type: 3,
			id: $("#id_d").val()
		},
		success: function (dataResult) {
			$('#deleteEmployeeModal').modal('hide');
			$("#" + dataResult).remove();
		}
	});
});

$(document).on("click", "#delete_multiple", function () {
	var user = [];
	$(".user_checkbox:checked").each(function () {
		user.push($(this).data('user-id'));
	});
	if (user.length <= 0) {
		alert("Please select records.");
	}
	else {
		WRN_PROFILE_DELETE = "Are you sure you want to delete " + (user.length > 1 ? "these" : "this") + " row?";
		var checked = confirm(WRN_PROFILE_DELETE);
		if (checked == true) {
			var selected_values = user.join(",");
			console.log(selected_values);
			$.ajax({
				type: "POST",
				url: "backend/save.php",
				cache: false,
				data: {
					type: 4,
					id: selected_values
				},
				success: function (response) {
					var ids = response.split(",");
					for (var i = 0; i < ids.length; i++) {
						$("#" + ids[i]).remove();
					}
				},
				error: function (xhr, status, error) {
			alert("An error occurred. HTTP Status: " + xhr.status + ". Status: " + status + ". Error: " + error);
		},
			});
		}
	}
});

0개의 댓글