--- id: php-ajax-poll title: "PHP AJAX Poll" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["PHP AJAX νˆ¬ν‘œ"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "ajax", "file-handling"] raw_sources: ["https://www.w3schools.com/php/php_ajax_poll.asp"] applied_in: [] github_commit: "" --- # [[PHP AJAX Poll]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) This poll persists its vote counts in a PLAIN TEXT FILE (`poll_result.txt`, format `"0||0"`), not a database β€” and the source explicitly warns to grant the WEB SERVER write access to that file, but NOT everyone, framing file permissions as the specific security concern for this simpler persistence approach. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`onclick` event** β€” triggers on radio button selection (vs. `onkeyup`/`onchange` in earlier chapters). [S1] - **Same AJAX skeleton** β€” `XMLHttpRequest` + `onreadystatechange`, now sending a `vote` parameter instead of a search string. [S1] - **File-based vote storage** β€” `poll_result.txt` stores counts as `"yesCount||noCount"`, parsed via `explode("||", $content[0])`. [S1] - **Read-modify-write cycle** β€” `file()` reads current counts, increments the relevant one, then `fopen()`+`fputs()`+`fclose()` overwrites the file with updated counts. [S1] - **Graphical percentage bars** β€” computed inline with `round($yes/($no+$yes),2)` and rendered as a proportionally-sized `` width. [S1] - **File permission warning** β€” only the web server (PHP process) should have write access to `poll_result.txt`, not all users. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Read-modify-write vote logic: `$vote = $_REQUEST['vote']; $content = file($filename); $array = explode("||", $content[0]); $yes = $array[0]; $no = $array[1]; if ($vote == 0) { $yes = $yes + 1; } if ($vote == 1) { $no = $no + 1; } $insertvote = $yes."||".$no; $fp = fopen($filename,"w"); fputs($fp,$insertvote); fclose($fp);`. [S1] - Percentage bar rendering: ` %`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **파일 μ“°κΈ° κΆŒν•œ κ²½κ³ **: poll_result.txt에 λŒ€ν•œ μ“°κΈ° κΆŒν•œμ€ μ›Ή μ„œλ²„(PHP)μ—λ§Œ λΆ€μ—¬ν•΄μ•Ό ν•˜λ©° λͺ¨λ“  μ‚¬μš©μžμ—κ²Œ μ—΄μ–΄μ£Όλ©΄ μ•ˆ λœλ‹€λŠ” 점이 λͺ…μ‹œμ μœΌλ‘œ 경고됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” ν…μŠ€νŠΈ 파일둜 νˆ¬ν‘œμˆ˜λ₯Ό μ €μž₯ν•˜κ³  κ·Έλž˜ν”½ λ§‰λŒ€λ‘œ λΉ„μœ¨μ„ ν‘œμ‹œν•˜λŠ” 것이 파일 기반 데이터 μ˜μ†ν™”μ˜ λŒ€ν‘œ μ‹€μ „ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Read-modify-write vote counting in a text file (PHP): ```php $vote = $_REQUEST['vote']; $content = file("poll_result.txt"); $array = explode("||", $content[0]); $yes = $array[0]; $no = $array[1]; if ($vote == 0) { $yes = $yes + 1; } if ($vote == 1) { $no = $no + 1; } $fp = fopen("poll_result.txt", "w"); fputs($fp, $yes."||".$no); fclose($fp); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP AJAX Live Search]], [[PHP Files Write]], [[PHP File Create]] - **μ°Έμ‘° λ§₯락:** AJAX μ„Ήμ…˜μ΄μž PHP νŠœν† λ¦¬μ–Ό μ „μ²΄μ˜ λ§ˆμ§€λ§‰ 챕터 β€” 파일 기반 데이터 μ €μž₯을 μ‹€μ „ ν™œμš©μœΌλ‘œ 마무리. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP - AJAX Poll β€” https://www.w3schools.com/php/php_ajax_poll.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP - AJAX Poll" page (Astra wiki-curation, P-Reinforce v3.1 format).