0

I have a separate PHP file containing a valid mysql query returning # of Rows representing my criteria (No. of active Notifications), and I have a JQUERY code to display notifications counter, but I only can use it statically with .text('5') for example. I want to be able to link the PHP MYSQL query result to the JQUERY output on browser.

notfcounter.php:

<?php
function getNotCount() {
    require_once $_SERVER['DOCUMENT_ROOT'] . 'info.php';
    $conn   = new mysqli($hn,$user,$pass,$db) or die("Unable to connect");
    $count = $_POST['action'];
    $sqlquery = "SELECT did FROM users WHERE users.uid=".$_SESSION['id'];
    $result = $dbc->query($sqlquery);
    while ($row = $result->fetch_assoc()) {
        $rows = $row['did'];
    }
    $countquery = "SELECT noname, noaction FROM notfs WHERE notfs.did=".$rows." AND notfstatus = 0";
    $result2 = $dbc->query($countquery);
    if (mysqli_num_rows($result2)!=0)
    {  
       $count = mysqli_num_rows($result2);
       echo '<a href="#">'.$count.'</a>';
    }
    else
    {
       $count = 0;
       echo '<a href="#">'.$count.'</a>';
    }
echo $count;
}
?>

JQUERY external file:

$(document).ready(function () {
    // ANIMATEDLY DISPLAY THE NOTF COUNTER.
    $('#notfs_Counter')
        .css({ opacity: 0 })
        //.text('5') this is what I use to display a counter statically
        $.ajax({
        type: 'post',
        url: 'notfcounter.php',
        data: {action: 'getNotCount'},
        success: function(data) {
            alert(data);
        }
    })
        .css({ top: '0px' })
        .animate({ top: '0px', opacity: 1 }, 500);
});
Samy
  • 63
  • 7
  • 1
    `success: function(data)`...the output from php will then be in `data` for you to use within the function. I suggest looking at a few more ajax examples to get the hang of it. – ADyson Nov 15 '22 at 17:40
  • first of all, thank you for your kind and humble reply. I appreciate it. secondly, I thought that (data) was meant to be passed for the PHP function, but my function has no parameters so I removed it. now I understand that it saves the OUTPUT from the PHP result. I will try something and update you. – Samy Nov 15 '22 at 17:44
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Nov 15 '22 at 17:44
  • thank you for the warning, I am already considering securing my code & queries later on, I am just trying to develop the core first :) – Samy Nov 15 '22 at 17:45
  • @ADyson, now the alert returns empty result, I tested the query itself and it returns correct values. what am I missing? I modified my code, kindly would you give it another look? – Samy Nov 15 '22 at 18:30

1 Answers1

1

after researching more about AJAX, I got to understand it better and knew where I was wrong. I had to adjust my code to reposition the $(selector). commands inside the AJAX success function call to properly display my data. Also, I didn't need a function on my PHP code, I removed the function and echo'd the needed variable for display $count, and modified AJAX action to count, code is below:

AJAX & JQUERY:

$.ajax({
            type: 'post',
            url: 'notfscounter.php',
            data: {action: 'count'},
            success: function(data) {
                //alert(data)
                $('#notfs_Counter').css({opacity: 0})
                $('#notfs_Counter').text(data)
                $('#notfs_Counter').css({ top: '0px'})
                $('#notfs_Counter').animate({ top: '0px', opacity: 1 }, 500);
            }
        });

PHP:

<?php
    require_once $_SERVER['DOCUMENT_ROOT'] . 'info.php';
    $conn   = new mysqli($hn,$user,$pass,$db) or die("Unable to connect");
    $sqlquery = "SELECT did FROM users WHERE users.uid=".$_SESSION['id'];
    $result = $dbc->query($sqlquery);
    while ($row = $result->fetch_assoc()) {
        $rows = $row['did'];
    }
    $countquery = "SELECT noname, noaction FROM notfs WHERE notfs.did=".$rows." AND notfstatus = 0";
    $result2 = $dbc->query($countquery);
    if (mysqli_num_rows($result2)!=0)
    {  
       $count = mysqli_num_rows($result2);
       echo '<a href="#">'.$count.'</a>';
    }
    else
    {
       $count = 0;
       echo '<a href="#">'.$count.'</a>';
    }
echo $count;
?>

special thanks to @ADyson for his encouraging & helpful guide.

Samy
  • 63
  • 7