mercredi 30 décembre 2020

differentiating between post requests

I have this jquery function which sends a POST request to the server

function loadInfo() {
    jQuery(function($) {
        $.ajax({
            method: "POST",
            url: "/admin.php",
            dataType: "json",
            success: function(data) {
                console.log(data);
                for (var i = 0; i < data.length; i++) {
                    setMarker(data, i, "red");
                    printInfo(data, i);
                }
            }
        })
    });
}

This is how the request gets handled

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    header('Content-Type: application/json');
    require 'private/database.php';

    $sql = "SELECT * FROM form";
    $result = mysqli_query($conn, $sql);

    $data = array();
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $data[] = $row;
        }
    }
    die(json_encode($data));
}

The code works, but what if I have send multiple POST requests to the same server? Is there a way to differentiate between them like how you would when handling HTML forms? e.g. If the of the button that submits the form is "submit_button"...

if (isset($_POST['submit_button'])) {
   ...
}



Aucun commentaire:

Enregistrer un commentaire