Home > Blockchain >  How To Target A Specific PHP Function Using Ajax
How To Target A Specific PHP Function Using Ajax

Time:11-09

I am creating a simple comment program. i want to use ajax to refresh the comments and total number of comments. Keeping both functions in a single file is not working for me.

here is my code:

HTML:

<h3></h3>
<ul>
        
</ul>

PHP: include.php

//for comments
function main(){
    try {
        $query = connect()->prepare("SELECT * FROM comments WHERE article_id = 1627359589");
        $query->execute();
        echo json_encode($query->fetchAll());

    } catch (PDOException $e) {
        die("database connection failed");
    }
}

// for total comment
function totalComment(){
        $sql ="SELECT * FROM comments WHERE article_id = 1627359589";
        $stmt = connect()->prepare($sql);
        $stmt->execute();
        $num = $stmt->rowCount();
        echo json_encode($num);
}

main();
totalComment();

AJAX:

// for comment
setInterval(displayComments, 2000);

function displayComments(){
       $.ajax({
            url: "include.php",
            type: "POST",
            dataType: "JSON",
            success: function(data){
                for (var i = 0; i < data.length; i  ) {
                    $("ul").append("<li>" data[i]   "</li>")
                }
            }
        })
}

// for total comments
setInterval(total, 2000);
function total(){
            $.ajax({
                url: "include.php",
                success: function(data){
                    $("h3").html(data);
                }
              
            })
    }

CodePudding user response:

Set the PHP to process a named POST item and use switch to determine which function to use:

<?php
    if( $SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['cmd'] )){
    

        function main(){
            try {
                $query = connect()->prepare("SELECT * FROM comments WHERE article_id = 1627359589");
                $query->execute();
                echo json_encode($query->fetchAll());

            } catch (PDOException $e) {
                die("database connection failed");
            }
        }

        function totalComment(){
                $sql ="SELECT * FROM comments WHERE article_id = 1627359589";
                $stmt = connect()->prepare($sql);
                $stmt->execute();
                $num = $stmt->rowCount();
                echo json_encode($num);
        }
        
        
        
        switch( $_POST['cmd'] ){
            case 'comments':
                main();
            break;
            case 'total':
                totalComment();
            break;
            case 'banana':
                banana();
            break;
        }
    }
    
?>

In your javascript set the same parameter in each request but with a different value: data:{cmd:'banana'}, etc

function displayComments(){
    $.ajax({
        url: "include.php",
        type: "POST",
        data:{cmd:'comments'},
        dataType: "JSON",
        success: function(data){
            for (var i = 0; i < data.length; i  ) {
                $("ul").append("<li>" data[i]   "</li>")
            }
        }
    })
}
function total(){
    $.ajax({
        url: "include.php",
        data:{cmd:'total'},
        success: function(data){
            $("h3").html(data);
        }
    })
}


setInterval(displayComments, 2000);
setInterval(total, 2000);
  • Related