1

I am making a site where a user submits an answer to a question and after that it adds a point to his $_SESSION and $_COOKIE variable, the problem is it does not update until he manually refreshes the page. How should i work this problem out?

Code:

    <?php
    if(!isset($_COOKIE['score']) AND !isset($_COOKIE['scoreright'])) {
        setcookie("score", 0, strtotime( '+1 year' ), "/");
        setcookie("scoreright", 0,  strtotime( '+1 year' ), "/");
    }
    $cscore = $_COOKIE['score'];
    $cscoreright = $_COOKIE['scoreright'];
    session_start();
    if(!isset($_SESSION['scoreright']) AND !isset($_SESSION['score'])) {
        $_SESSION['score'] = 0;
        $_SESSION['scoreright'] = 0;
    }
    if(isset($_POST['check']) AND isset( $_POST['ODP'])) {
        if($_POST['ODP'] == $_POST['POPODP']) {
            ++$cscore;
            ++$cscoreright;
            setcookie("score", $cscore, strtotime( '+1 year' ), "/");
            setcookie("scoreright", $cscoreright,  strtotime( '+1 year' ), "/");
            $_SESSION['score']++;
            $_SESSION['scoreright']++;
            //echo "Poprawna odpowiedź";
    } else {
        //echo "Zła odpowiedź";
        ++$cscore;
        setcookie("score", $cscore, strtotime( '+1 year' ), "/");
        $_SESSION['score']++;
    }
    }
?>

Code:

<script type="text/javascript">
    $(".submit").click(function() {
        var odp = $('input[name=ODP]:checked').val();
        var popodp = $('input[name=POPODP]').val();
        var ch = true;
        $.ajax({
            type: "POST",
            url: "includes/points.php",
            data: { ODP:odp, POPODP: popodp, check: ch },
            success: function(result){
                toastr["success"]("Poprawna odpowiedź.");
            }
            });
      });
    </script>

Code:

        <div class="question">Question</div>
        <input type="radio" id="odp1" value="1" name="ODP" hidden>
        <label for="odp1">2</label>
        <input type="radio" id="odp2" value="2" name="ODP" hidden>
        <label for="odp2">1</label>
        <input type="radio" id="odp3" value="3" name="ODP" hidden>
        <label for="odp3">3</label>
        <input type="radio" id="odp4" value="4" name="ODP" hidden>
        <label for="odp4">4</label>
        <input type="text" value="2" name="POPODP" hidden>
        <input class="btn submit" type="submit" name="check" value="ODPOWIADAM">    

Code:

    <div class="session-cookie">
        <div class="stats">
            <b>Odpowiedzi ogółem:</b>
            <span style="color:#64B5F6">odpowiedzi </span><b><?php echo $cscore; ?></b>,
            <span style="color:#81C784;">poprawnych</span> <b><?php echo $cscoreright;?></b> i <span style="color:#E57373;">niepoprawnych</span> <?php echo $cscore - $cscoreright;?>.
          </div>
          <div class="stats">
            <b>W tej sesji:</b>
            <span style="color:#64B5F6">odpowiedzi </span><b><?php echo $_SESSION['score']; ?></b>,
            <span style="color:#81C784;">poprawnych</span> <b><?php echo $_SESSION['scoreright'];?></b> i <span style="color:#E57373;">niepoprawnych</span> <?php echo $_SESSION['score'] - $_SESSION['scoreright'];?>.
        </div>
    </div>

I appreciate any kind of help. Thanks in advance.

// Odpowiedź = Answer, Zła = Bad, Poprawna = Good, ODPOWIADAM = I answer, niepoprawnych = Bad Answers (in this meaning).

Ramkee
  • 900
  • 1
  • 10
  • 27

0 Answers0