2014-12-15 2 views
1

버튼 클릭 수를 계산하는 PHP 코드를 실행하려고합니다. 1로 증가한 다음 계산되지 않습니다. 여기 내 코드는 다음과 같습니다.버튼 카운터 용 PHP 코드

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $counter = isset($_POST['counter']) ? $_POST['counter'] : 0; 
    if(isset($_POST["button"])){ 
    $counter++; 
    echo $counter; 
    } 
} 
?> 
<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = post> 
    <input type = "submit" name = "button" value = "vote" > 
</form> 

PHP 전문가가 아니므로 어디에서 잘못 됐는지 알려주실 수 있습니까?

감사

+0

PHP가 아닌 j를 사용하여 버튼을 클릭해야합니다. PHP 값을 txt 파일이나 DB에 저장하지 않는 한. –

+0

양식에'$ _POST [ 'counter']'값을 포함하지 않았습니다. – showdev

+0

또는 적어도 $ _SESSION을 사용하여 단일 사용자 – Razorphyn

답변

0

사용이 PHP : 개수에

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $counter = isset($_POST['counter']) ? $_POST['counter'] : 0; 
    if(isset($_POST["button"])){ 
    $counter++; 
    echo $counter; 
    } 
} 
?> 
<form action = "<?php echo $_SERVER["PHP_SELF"]; ?>" method = post> 
    <input type = "hidden" name = "counter" value = "<?php echo $counter; ?>" /> 
    <input type = "submit" name = "button" value = "vote" /> 
</form> 
0

이를 사용세션. 이것은 각 사용자에 대해 별도의 수를 저장합니다. 모든 사용자가 공유하는 하나의 단일 계산을 원하면 데이터베이스에 저장해야합니다.

<?php 
// Start the session 
session_start(); 
// Make sure a session variable exists 
if (!isset($_SESSION['count'])) { 
    $_SESSION['count'] = 0; 
} 
// Check to see if a vote has been submitted 
$vote = isset($_POST['button']) ? $_POST['button'] : false; 
if ($vote) { 
    // Increment the vote 
    $_SESSION['count']++; 
} 
?> 
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post"> 
    <input type="submit" name="button" value="Vote"> 
</form> 
+0

응답 해 주셔서 감사합니다. – user3262134

0

스토어 : 또한

<?php 
if(isset($_POST["button"])){ 
    $counter++; 
    echo counter; 
} 
?> 

이 시도 오프닝 <form> 요소

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">