Edy
Edy

Reputation: 35

How to refresh 1 single value from database without refresh web page in file PHP

<?php
    session_start();

    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'kumkum09';
    $dbname =   'eoffice_db';
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if(! $conn )
    {
        die('Could not connect: ' . mysqli_error());
    }

    if (isset($_SESSION['user']) == '') {

        // Fungsi Validasi Login dan Create session
        if(isset($_POST['user'])){
        $username = mysqli_real_escape_string($conn, $_POST['user']);
        $password = mysqli_real_escape_string($conn, $_POST['pass']);

        $query      = mysqli_query($conn, "SELECT * FROM users WHERE  password=md5('$password') and username='$username'");
        $row          = mysqli_fetch_array($query);
        $num_row    = mysqli_num_rows($query);

        if ($num_row > 0)
        {
            $_SESSION['user']=$row['username'];
            $user=$_SESSION['user'];

            // echo "User: ".$user."<br>";

            // Fungsi Cek Token dan Create Token
            $cektoken = "SELECT * FROM users WHERE username='$user'";
            $resultcekuser = $conn->query($cektoken);
            $rowtoken = $resultcekuser->fetch_object();
            $tokennye = $rowtoken->token;

            if($tokennye==""){
                $tokenz = md5($user.time().rand());
                $updttokenuser = "UPDATE users SET token='$tokenz' WHERE username='$user'";
                $prosesupdttokenuser = $conn->query($updttokenuser);
            }
            $createsessiontoken = mysqli_query($conn, "SELECT * FROM users WHERE username='$user'");
            $sessiontoken = mysqli_fetch_array($createsessiontoken);
            $_SESSION['token']  = $sessiontoken['token'];
            header("location: tes.php");
        }
        }
        else
        {
            // header("location: index.php");
        }
    }

    else
    {
        $user  = $_SESSION['user'];
        $token = $_SESSION['token'];

        // Counting Jumlah Notifikasi Memo Belum Baca
        $notifmemo = "SELECT * FROM memo WHERE username_tujuan='$user' and status='Belum Dibaca'";
        $resultnotifmemo = $conn->query($notifmemo);
        $rowcount=mysqli_num_rows($resultnotifmemo);
    }
?>

The code above I put in the file with name tes.php, and I include in index.php. I call $rowcount to display the number as amount of notification for new mail.

The question is, how I can refresh the value of $rowcount without refresh the index.php page? Please help me, Thank You.

Upvotes: 1

Views: 92

Answers (1)

Dani
Dani

Reputation: 925

You can use Ajax for write a function and call it after some time then send ajax request to your tes.php file count your data and show the response in your page where you want: i give you ajax example

     $(function(){
          setTimeout(notification,30000);

         function notification(){
          $.ajax({
              data : 'get',
              dataType : 'html',
              url : 'test.php' 
              data : {check_data : 1},
              success : function(data){
                     $('#id of your div').html(data);
              },
              error : function(){
                   console.log('Error');
            }
});
}


    });

In you test.php page count your record and just echo or return it

Upvotes: 1

Related Questions