0

PHP Post doesn't receive jQuery ajax post value

I have created an ajax POST with jQuery to obtain the ID of a div and pass it to a PHP function. The function of php is in a folder phpFunction taht contains the file phpFunc.php in the same root of index.php. The post request is:

$(document).ready(function() {
            $('.openModal').click(function() {
                var divId = $(this).attr('id');
                // Invia l'ID al server tramite richiesta AJAX
                $.ajax({
                    type: 'POST',
                    url: 'http://localhost/icollegati/phpFunction/phpFunc.php',
                    data: {divId: divId},
                    success: function(response) {
                        // Gestisci la risposta del server
                        console.log("Risposta",response);
                    }
                });
            });
        });

the PHP function is:

function FetchIdNumber(){
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Verifica se il parametro "divId" è stato inviato tramite POST
    if (isset($_POST['divId'])) {
        $id = $_POST['divId'];
        echo "ID: " . $id;
        return $id;
    }
  }
};

Thanks in adavance for the help.

1 Answers1

-2

you need to call the FetchIdNumber function in your PHP code.

function FetchIdNumber(){
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // Verifica se il parametro "divId" è stato inviato tramite POST
        if (isset($_POST['divId'])) {
            $id = $_POST['divId'];
            echo "ID: " . $id;
            return $id;
        }
    }
}

// Call the function
FetchIdNumber();

Make sure to place the function call FetchIdNumber(); after the function definition.

Additionally, please ensure that the path to your PHP file is correct. In your AJAX code, you have specified the URL as http://localhost/icollegati/phpFunction/phpFunc.php. Make sure that this path is accurate and matches the actual location of the PHP file on your server.