繁体   English   中英

location.reload()与我的数据库有所不同

[英]location.reload() make a difference with my DB

我实现了一个用于费率系统的代码,但是当我选择投票时,什么都没有改变,当我再次选择投票时,就可以了! 但是,当我在phpmyadmin上看到我的数据库时,投票计数器比我在页面上“回显”的投票计数器多了1倍。为什么会有这种差异1?

我的note.js

$(function(){
    $('.star').on('mouseover', function(){
        var indice = $('.star').index(this);
        $('.star').removeClass('full');
        for(var i = 0; i<= indice; i++){
            $('.star:eq('+i+')').addClass('full');  
        }
    }); 

    $('.star').on('mouseout', function(){
        $('.star').removeClass('full');
        });

    var average = $('.average').attr('data-average');
    function avaliacao(average){
        average = (Number(average)*20);
        $('.barra .bg').css('width', 0);
        $('.barra .bg').animate({width: average+'%'}, 500);
    }
    avaliacao(average);


    $('.star').on('click', function(){
        var artigoId = $('.artigoDados').attr('data-id');
        var ponto = $(this).attr('id');
        location.reload();
        $.post('sys/votar.php',{votar: 'sim', artigo: artigoId, ponto: ponto}, function(retorno){
        avaliacao(retorno.average);
        $('p.votos span').html(retorno.votos);

        }, 'jSON');

    });

});

我的html代码:

<?php 

$bdd = new PDO('mysql:host=localhost;dbname=notation', 'root', 'root');

?>

<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/note.js"></script>
</head>


<body>

<?php
    $artigoId = (int)$_GET['artigo'];

    $artigos = $bdd->prepare('SELECT * FROM sys_note WHERE id_recette = ?');
    $artigos->execute(array($artigoId));
    while($row = $artigos->fetchObject()){
    echo '<h1>'.$row->titre_recette.'</h1>';
    $calculo = ($row->pontos == 0) ? 0 : round(($row->pontos/$row->votos), 1);
    echo '<span class="average" data-average="'.$calculo.'"></span>';
    echo '<span class="artigoDados" data-id="'.$row->id_recette.'"></span>';




?>

<div class="barra">
    <span class="bg"></span>
    <div class="estrelas">
    <?php for($i=1; $i<=5; $i++): ?>
    <span class ="star" id="<?php echo $i;?>">
    <span class="starAbsolute"></span>
    </span>
    <?php endfor;?>
    </div>
</div>
<p class="votos"><span><?php echo $row->votos;?></span>votes</p>
<?php }?>

</body>
</html>

votar.php

<?php 

$bdd = new PDO('mysql:host=localhost;dbname=notation', 'root', 'root');

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $artigo = (int)$_POST['artigo'];    
    $pontos = $_POST['ponto'];

    $pegaArtigo = $bdd->prepare('SELECT * FROM sys_note WHERE id_recette = ?');
    $pegaArtigo->execute(array($artigo));

    while($row = $pegaArtigo->fetchObject()){
        $votosUpd = $row->votos+1;
        $pontosUpd = $row->pontos+$pontos;
        $average = round(($pontosUpd/$votosUpd), 1);
        $update = $bdd->prepare('UPDATE sys_note SET votos = ?, pontos = ? WHERE id_recette = ?');
        if($update->execute(array($votosUpd, $pontosUpd, $artigo))){
        die(json_encode(array('average' => $average, 'votos' => $votosUpd)));   

        }

    }

}

?>

你需要做location.reload(); 在您发布帖子之后,而不是之前。

$.post('sys/votar.php',{votar: 'sim', artigo: artigoId, ponto: ponto}, function(retorno){
    location.reload();
    }, 'jSON');

由于您仍在重新加载页面,因此没有必要在回调内部做额外的工作。 虽然,我建议您通过ajax而不是粗略的location.reload();来更改页面上的元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM