📖
그누보드5 에서 자주 쓰는 함수 alert() confirm() 나만의 confirm() 전용 PHP 함수 만들기
페이지 정보
본문
그누보드5 (Gnuboard 5) 에서는 자바스크립트 alert() confirm() 을 PHP 함수로 편리하게 출력할 수 있도록 **도움 함수(helper)**를 미리 정의해놨어요.
alert() 함수는 그누보드의 lib/common.lib.php에 정의되어 있어요.
✨ 사용 방법:[code]alert("이미 존재하는 URL 입니다.");[/code]
[code]confirm("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]이렇게만 쓰면 됩니다! 😄
PHP에서 이걸 호출하면, 내부적으로 alert용 confirm용 HTML과 JavaScript가 자동으로 출력돼요.
💡 동작 원리 (간단 설명):
common.lib.php 안에는 이런 식으로 정의되어 있습니다.[code]function alert($msg='', $url='', $error=true)
{
...
echo "<script>alert('$msg');";
if ($url)
echo "location.replace('$url');";
else
echo "history.go(-1);";
echo "</script>";
exit;
}
confirm() 함수도동일한 방법으로 포함되어있음
[/code]
✅ 응용 예:
1. 단순 경고 + 뒤로가기[code]alert("이미 존재하는 URL 입니다.");[/code]➡ alert() 후 자동으로 이전 페이지로 돌아갑니다 (history.go(-1) 포함됨)
2. 경고 후 특정 페이지로 이동[code]alert("등록되었습니다.", "/bbs/board.php?bo_table=notice");[/code]➡ 경고창 후 해당 URL로 이동
나만의 confirm() 전용 PHP 함수 만들기
✅ 목표
PHP에서 다음처럼 사용할 수 있도록 만들기[code]confirm("정말 삭제하시겠습니까?", "/delete_ok.php", "/list.php");[/code]➡ "확인" 누르면 /delete_ok.php로 이동
"취소" 누르면 /list.php로 이동
🛠️ 1단계: common.lib.php에 함수 추가
/lib/common.lib.php 파일 열고, 아래 함수 추가하세요.[code]function confirm_msg($msg, $yes_url = '', $no_url = '') {
echo "<script>
if (confirm('".str_replace("'", "'", $msg)."')) {
location.href = '$yes_url';
} else {
location.href = '$no_url';
}
</script>";
exit;
}[/code]✅ exit; 포함되어 있어서, confirm 후 나머지 PHP는 실행되지 않아요.
✅ 사용 예시[code]confirm_msg("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]
✅ 취소 시 뒤로 가고 싶다면?[code]confirm_msg("정말 삭제하시겠습니까?", "/delete.php?no=123", "history.back()");[/code]no_url에 "history.back()" 이렇게 쓰면 JS 코드가 그대로 들어가게 하려면 약간 수정이 필요해요.
그럴 땐 아래 버전 쓰세요 👇
🎯 JS 코드로 이동하려면 이 버전 사용[code]function confirm_msg($msg, $yes_url = '', $no_action = 'history.back();') {
echo "<script>
if (confirm('".str_replace("'", "'", $msg)."')) {
location.href = '$yes_url';
} else {
$no_action
}
</script>";
exit;
}[/code]
사용 예:[code]confirm_msg("삭제할까요?", "/delete.php?no=123", "history.back();");[/code]
confirm()을 브라우저 기본 팝업 대신, 모달(confirm창 형태)로 예쁘게 커스터마이징 하기
✅ 목표:
PHP에서 confirm_modal("메시지", "확인 URL", "취소 URL"); 호출
HTML + JS로 모달창 띄우기
확인/취소 버튼에 따라 이동
🔧 1단계: confirm_modal() 함수 만들기 (PHP)[code]function confirm_modal($msg, $yes_url = '', $no_url = '') {
echo <<<HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>확인</title>
<style>
body { font-family: sans-serif; background: rgba(0,0,0,0.5); margin:0; padding:0; }
.modal-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
display: flex; justify-content: center; align-items: center;
background: rgba(0,0,0,0.5); z-index: 9999;
}
.modal {
background: #fff; padding: 30px; border-radius: 12px; text-align: center;
box-shadow: 0 10px 30px rgba(0,0,0,0.2); max-width: 400px;
}
.modal-message {
margin-bottom: 20px; font-size: 18px;
}
.modal-buttons button {
padding: 10px 20px; margin: 0 10px;
border: none; border-radius: 6px; font-size: 16px; cursor: pointer;
}
.btn-yes { background: #3a8afd; color: white; }
.btn-no { background: #ccc; }
</style>
</head>
<body>
<div class="modal-overlay">
<div class="modal">
<div class="modal-message">{$msg}</div>
<div class="modal-buttons">
<button class="btn-yes" onclick="location.href='{$yes_url}'">확인</button>
<button class="btn-no" onclick="location.href='{$no_url}'">취소</button>
</div>
</div>
</div>
</body>
</html>
HTML;
exit;
}[/code]
✅ 사용 방법 (예시)[code]confirm_modal("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]
💡 추가 팁
취소 시 history.back() 하고 싶다면:[code]<button class="btn-no" onclick="history.back();">취소</button>[/code]위 PHP 함수 내 $no_url이 "history.back();"일 경우 자동으로 JS 코드로 처리하는 조건 넣어도 됩니다.
🧼 깔끔하게 관리하려면?
이 모달 confirm은 **별도 PHP 파일(confirm_modal.php)**로 따로 빼서
필요할 때 include하거나 함수만 로드해도 OK입니다!
모달을 "페이드인" 대신 슬라이드로 부드럽게 등장하게 만들어봅시다!
✅ 목표:
confirm_modal()을 모달로 띄우되
위에서 아래로 슬라이드 인 애니메이션 추가
닫을 땐 사라지게 해도 되고 페이지 이동하니까 바로 이동 처리
✅ 슬라이드 애니메이션 포함 confirm_modal()[code]function confirm_modal($msg, $yes_url = '', $no_url = '') {
echo <<<HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>확인</title>
<style>
body { font-family: sans-serif; background: rgba(0,0,0,0.5); margin:0; padding:0; overflow: hidden; }
.modal-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
display: flex; justify-content: center; align-items: center;
background: rgba(0,0,0,0.5); z-index: 9999;
}
.modal {
background: #fff; padding: 30px; border-radius: 12px; text-align: center;
box-shadow: 0 10px 30px rgba(0,0,0,0.2); max-width: 400px;
transform: translateY(-200px);
opacity: 0;
animation: slideIn 0.5s ease forwards;
}
@keyframes slideIn {
to {
transform: translateY(0);
opacity: 1;
}
}
.modal-message {
margin-bottom: 20px; font-size: 18px;
}
.modal-buttons button {
padding: 10px 20px; margin: 0 10px;
border: none; border-radius: 6px; font-size: 16px; cursor: pointer;
}
.btn-yes { background: #3a8afd; color: white; }
.btn-no { background: #ccc; }
</style>
</head>
<body>
<div class="modal-overlay">
<div class="modal">
<div class="modal-message">{$msg}</div>
<div class="modal-buttons">
<button class="btn-yes" onclick="location.href='{$yes_url}'">확인</button>
<button class="btn-no" onclick="location.href='{$no_url}'">취소</button>
</div>
</div>
</div>
</body>
</html>
HTML;
exit;
}[/code]
✅ 예시 사용[code]confirm_modal("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]
🎉 결과
페이지 열리면 위에서 아래로 "슥~" 내려오는 슬라이드 모달 confirm
확인 클릭 → yes_url로 이동
취소 클릭 → no_url로 이동 (예: history.back())
alert() 함수는 그누보드의 lib/common.lib.php에 정의되어 있어요.
✨ 사용 방법:[code]alert("이미 존재하는 URL 입니다.");[/code]
[code]confirm("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]이렇게만 쓰면 됩니다! 😄
PHP에서 이걸 호출하면, 내부적으로 alert용 confirm용 HTML과 JavaScript가 자동으로 출력돼요.
💡 동작 원리 (간단 설명):
common.lib.php 안에는 이런 식으로 정의되어 있습니다.[code]function alert($msg='', $url='', $error=true)
{
...
echo "<script>alert('$msg');";
if ($url)
echo "location.replace('$url');";
else
echo "history.go(-1);";
echo "</script>";
exit;
}
confirm() 함수도동일한 방법으로 포함되어있음
[/code]
✅ 응용 예:
1. 단순 경고 + 뒤로가기[code]alert("이미 존재하는 URL 입니다.");[/code]➡ alert() 후 자동으로 이전 페이지로 돌아갑니다 (history.go(-1) 포함됨)
2. 경고 후 특정 페이지로 이동[code]alert("등록되었습니다.", "/bbs/board.php?bo_table=notice");[/code]➡ 경고창 후 해당 URL로 이동
나만의 confirm() 전용 PHP 함수 만들기
✅ 목표
PHP에서 다음처럼 사용할 수 있도록 만들기[code]confirm("정말 삭제하시겠습니까?", "/delete_ok.php", "/list.php");[/code]➡ "확인" 누르면 /delete_ok.php로 이동
"취소" 누르면 /list.php로 이동
🛠️ 1단계: common.lib.php에 함수 추가
/lib/common.lib.php 파일 열고, 아래 함수 추가하세요.[code]function confirm_msg($msg, $yes_url = '', $no_url = '') {
echo "<script>
if (confirm('".str_replace("'", "'", $msg)."')) {
location.href = '$yes_url';
} else {
location.href = '$no_url';
}
</script>";
exit;
}[/code]✅ exit; 포함되어 있어서, confirm 후 나머지 PHP는 실행되지 않아요.
✅ 사용 예시[code]confirm_msg("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]
✅ 취소 시 뒤로 가고 싶다면?[code]confirm_msg("정말 삭제하시겠습니까?", "/delete.php?no=123", "history.back()");[/code]no_url에 "history.back()" 이렇게 쓰면 JS 코드가 그대로 들어가게 하려면 약간 수정이 필요해요.
그럴 땐 아래 버전 쓰세요 👇
🎯 JS 코드로 이동하려면 이 버전 사용[code]function confirm_msg($msg, $yes_url = '', $no_action = 'history.back();') {
echo "<script>
if (confirm('".str_replace("'", "'", $msg)."')) {
location.href = '$yes_url';
} else {
$no_action
}
</script>";
exit;
}[/code]
사용 예:[code]confirm_msg("삭제할까요?", "/delete.php?no=123", "history.back();");[/code]
confirm()을 브라우저 기본 팝업 대신, 모달(confirm창 형태)로 예쁘게 커스터마이징 하기
✅ 목표:
PHP에서 confirm_modal("메시지", "확인 URL", "취소 URL"); 호출
HTML + JS로 모달창 띄우기
확인/취소 버튼에 따라 이동
🔧 1단계: confirm_modal() 함수 만들기 (PHP)[code]function confirm_modal($msg, $yes_url = '', $no_url = '') {
echo <<<HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>확인</title>
<style>
body { font-family: sans-serif; background: rgba(0,0,0,0.5); margin:0; padding:0; }
.modal-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
display: flex; justify-content: center; align-items: center;
background: rgba(0,0,0,0.5); z-index: 9999;
}
.modal {
background: #fff; padding: 30px; border-radius: 12px; text-align: center;
box-shadow: 0 10px 30px rgba(0,0,0,0.2); max-width: 400px;
}
.modal-message {
margin-bottom: 20px; font-size: 18px;
}
.modal-buttons button {
padding: 10px 20px; margin: 0 10px;
border: none; border-radius: 6px; font-size: 16px; cursor: pointer;
}
.btn-yes { background: #3a8afd; color: white; }
.btn-no { background: #ccc; }
</style>
</head>
<body>
<div class="modal-overlay">
<div class="modal">
<div class="modal-message">{$msg}</div>
<div class="modal-buttons">
<button class="btn-yes" onclick="location.href='{$yes_url}'">확인</button>
<button class="btn-no" onclick="location.href='{$no_url}'">취소</button>
</div>
</div>
</div>
</body>
</html>
HTML;
exit;
}[/code]
✅ 사용 방법 (예시)[code]confirm_modal("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]
💡 추가 팁
취소 시 history.back() 하고 싶다면:[code]<button class="btn-no" onclick="history.back();">취소</button>[/code]위 PHP 함수 내 $no_url이 "history.back();"일 경우 자동으로 JS 코드로 처리하는 조건 넣어도 됩니다.
🧼 깔끔하게 관리하려면?
이 모달 confirm은 **별도 PHP 파일(confirm_modal.php)**로 따로 빼서
필요할 때 include하거나 함수만 로드해도 OK입니다!
모달을 "페이드인" 대신 슬라이드로 부드럽게 등장하게 만들어봅시다!
✅ 목표:
confirm_modal()을 모달로 띄우되
위에서 아래로 슬라이드 인 애니메이션 추가
닫을 땐 사라지게 해도 되고 페이지 이동하니까 바로 이동 처리
✅ 슬라이드 애니메이션 포함 confirm_modal()[code]function confirm_modal($msg, $yes_url = '', $no_url = '') {
echo <<<HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>확인</title>
<style>
body { font-family: sans-serif; background: rgba(0,0,0,0.5); margin:0; padding:0; overflow: hidden; }
.modal-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
display: flex; justify-content: center; align-items: center;
background: rgba(0,0,0,0.5); z-index: 9999;
}
.modal {
background: #fff; padding: 30px; border-radius: 12px; text-align: center;
box-shadow: 0 10px 30px rgba(0,0,0,0.2); max-width: 400px;
transform: translateY(-200px);
opacity: 0;
animation: slideIn 0.5s ease forwards;
}
@keyframes slideIn {
to {
transform: translateY(0);
opacity: 1;
}
}
.modal-message {
margin-bottom: 20px; font-size: 18px;
}
.modal-buttons button {
padding: 10px 20px; margin: 0 10px;
border: none; border-radius: 6px; font-size: 16px; cursor: pointer;
}
.btn-yes { background: #3a8afd; color: white; }
.btn-no { background: #ccc; }
</style>
</head>
<body>
<div class="modal-overlay">
<div class="modal">
<div class="modal-message">{$msg}</div>
<div class="modal-buttons">
<button class="btn-yes" onclick="location.href='{$yes_url}'">확인</button>
<button class="btn-no" onclick="location.href='{$no_url}'">취소</button>
</div>
</div>
</div>
</body>
</html>
HTML;
exit;
}[/code]
✅ 예시 사용[code]confirm_modal("정말 삭제하시겠습니까?", "/delete.php?no=123", "/list.php");[/code]
🎉 결과
페이지 열리면 위에서 아래로 "슥~" 내려오는 슬라이드 모달 confirm
확인 클릭 → yes_url로 이동
취소 클릭 → no_url로 이동 (예: history.back())
댓글목록
등록된 댓글이 없습니다.
![]() ![]() |