티스토리 뷰

라이믹스

라이믹스 입력폼

xemaker 2026. 3. 10. 10:40

root에 insert_form 이라는 디렉토리를 만든다.

insert_form.html 파일을 생성한다.

<form id="myCustomForm">
    <input type="text" name="title" placeholder="제목" required><br>
    <textarea name="content" placeholder="내용" required></textarea><br>
    <input type="text" name="nick_name" placeholder="닉네임">
    <input type="password" name="password" placeholder="비밀번호">
    <button type="button" onclick="submitToRhymix()">등록하기</button>
</form>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function submitToRhymix() {
    var formData = jQuery('#myCustomForm').serialize();
    
    console.log("데이터 전송 시작...", formData); // 확인용

    jQuery.ajax({
        type: 'POST',
        url: './insert_form/insert_proc.php', // PHP 파일 경로가 정확한지 확인!
        data: formData,
        dataType: 'json',
        success: function(res) {
            console.log("서버 응답:", res);
            if(res.error == 0) {
                alert("완료되었습니다.");
                location.reload();
            } else {
                alert("실패: " + res.message);
            }
        },
        error: function(xhr, status, error) {
            console.error("에러 발생:", error);
            console.log("상세 내용:", xhr.responseText);
            alert("통신 오류가 발생했습니다. 개발자 도구(F12)를 확인하세요.");
        }
    });
}
</script>

insert_proc.php 파일을 생성한다.

<?php
define('__XE__', true);
require_once('../config/config.inc.php');
$oContext = &Context::getInstance();
$oContext->init();

// 1. 데이터 받기
$title = $_POST['title'];
$content = $_POST['content'];
$nick_name = $_POST['nick_name'] ?: '비회원';
$password = $_POST['password'] ?: '1234';
$module_srl = 51; 

if(!$title || !$content) {
    die(json_encode(array('error' => -1, 'message' => '데이터 누락')));
}

// 2. DB 객체 및 시퀀스 생성
$oDB = DB::getInstance();
$document_srl = getNextSequence();
$regdate = date('YmdHis');

// 3. 쿼리 실행 (오류 났던 allow_comment 컬럼 제거)
$query = "INSERT INTO documents 
          (document_srl, module_srl, title, content, user_id, nick_name, password, regdate, last_update, list_order, update_order, comment_status) 
          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$args = array(
    $document_srl, 
    $module_srl, 
    $title, 
    $content, 
    0, // 비회원
    $nick_name, 
    $password, 
    $regdate, 
    $regdate, 
    $document_srl * -1, 
    $document_srl * -1,
    'ALLOW' // comment_status 값
);

$stmt = $oDB->prepare($query);
$result = $stmt->execute($args);

// 4. 결과 출력
header('Content-Type: application/json');
if($result) {
    echo json_encode(array('error' => 0, 'message' => 'success'));
} else {
    echo json_encode(array('error' => -1, 'message' => 'DB 최종 입력 실패'));
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/03   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함