Pagina 1 din 1

Adaugare puncte la terminare timp fara refresh pagina

Scris: Dum Mai 19, 2013
de k0rupe
Salut
cum asi putea sa fac, cand accesez pagina sa nu mai trebuiasca sa dau refresh pentru a porni timpu(vreau sa apara direct fara refreshuri).
Daca se poate dupa ce se termina timpu si primesc punctele sa reinceapa iar.

intru pe pagina respectiva si imi apare "munca se va relua dupa urmatoru refresh"
ii dau refresh pentru a porni cronometru pana primesc punctele, dupa terminare trebuia sa ii dau iar de 2 ori refresh pentru a reincepe punctarea

Cod: Selectaţi tot

        case 7:
        //recompensa
            $usery = $_GET['user'];
            $aur = 5; // punctele primite
            $god = mysql_query("UPDATE users SET puncte = puncte + '".$aur."' WHERE user_name = '".$usery."'");
            
                if($god){
                    echo '1';
                } else {
                    echo '0';
                }
        break;

        case 8:
        //recompensa
            $usery = $_GET['user'];
            $started = time()+30; //60 = 1 min
            $god = mysql_query("UPDATE users SET work_pct = '".$started."' WHERE user_name = '".$usery."'");
            
                if($god){
                    echo '1';
                } else {
                    echo '0';
                }
        break; 
Si:

Cod: Selectaţi tot

<?php
                                        // include 'database.php';
                                            $flp = mysql_query("SELECT * FROM users WHERE user_name = '".$usernames."'");
                                            $player = mysql_fetch_array($flp);
                                            $pct = $player['puncte'];
                                            $f = $player['work_pct'];
                                            
                                            if(($f > 0) && ($f < time()) && ($f != 0)){
                                    ?>
                                        <script type="text/javascript">
                                            $(document).ready(function(){
                                                var userl = $('#username_k').text();
                                                $.ajax({
                                                    type:'POST',
                                                    url:'add_file.php?cod=8&user='+userl,
                                                    Async:false,
                                                    cache:false,
                                                    success:function(datau){
                                                        if(datau == 1){
                                                            alert('munca se va relua dupa urmatoru refresh');
                                                        }
                                                    }
                                                });
                                            });
                                        </script>
                                    <?php
                                            } else
                                            if($f > 0){
                                                $dog = $f - time();
                                                $done_work = date('H:i:s',$dog);
                                    ?>
                                    <script type="text/javascript">
                                        function work(ile){
                                            var ora = Math.floor(ile/3600);
                                            var minute = Math.floor((ile-ora*3600)/60);
                                            var secunde = ile-minute*60-ora*3600;
                                            if(ora<10){ ora = '0'+ora; }
                                            if(minute<10){ minute = '0'+minute; }
                                            if(secunde<10){ secunde = '0'+secunde; }

                                            if(ile > 0){
                                                ile--;
                                                document.getElementById("ceasp").innerHTML = ora+':'+minute+':'+secunde;
                                                setTimeout("work("+ile+")", 1000);
                                            } else {
                                                //alert('terminat');
                                                $(document).ready(function(){
                                                        var userl = $('#username_k').text();
                                                            $.ajax({
                                                                type:'POST',
                                                                url:'add_file.php?cod=7&user='+userl,
                                                                Async:false,
                                                                cache:false,
                                                                success:function(data){
                                                                    if(data == 1){
                                                                        alert('ai primit recompensa');
                                                                    }
                                                                }
                                                            });
                                                });
                                            }
                                        }
                                    </script>
                                    
                                    <li class="header">(<?php echo $pct; ?>)<strong>Puncte - Timp:&nbsp;<span id="ceasp"></span></strong></li>
                                    <script type="text/javascript">window.onload = work("<?php print $dog; ?>");</script>
                                    
                                    <?php
                                            } else {
                                    ?>
                                        <script type="text/javascript">
                                            $(document).ready(function(){
                                                var userl = $('#username_k').text();
                                                $.ajax({
                                                    type:'POST',
                                                    url:'add_file.php?cod=8&user='+userl,
                                                    Async:false,
                                                    cache:false,
                                                    success:function(datau){
                                                        if(datau == 1){
                                                            alert('cont nou - munca se va relua dupa urmatoru refresh');
                                                        }
                                                    }
                                                });
                                            });
                                        </script>
                                    <li class="header">(<?php echo 'null'; ?>)<strong>Puncte - Timp:&nbsp;<span id="ceasp">null</span></strong></li>
                                    <script type="text/javascript">window.onload = work("<?php print $dog; ?>");</script>
                                    <?php
                                            }
                                    ?>

Adaugare puncte la terminare timp fara refresh pagina

Scris: Dum Mai 19, 2013
de MarPlo
Salut
In general, ca sa executi un script de pe server fara Refresh la pagna, se foloseste Ajax.
Pentru ce vrei tu sa faci, trebuie prea multe modificari in script, si sa stii bine JavaScript.
Ca idee, cand timpu se termina, se apeleaza o functie Ajax care acceseaza fis8erul php ce adauga punctele.
Dupa raspunsul de la server, se actualizeaza cu JavaScript punctele si in pagina si timpul care sa inceapa iar.