从Ajax发送的PHP中检索数据

时间:2017-06-09 14:00:41

标签: php jquery ajax

我试图访问从AJAX发送的php文件中的变量。我尝试了$ _POST [' category_id'],如下所示,但没有运气。 category_id变量设置为某个值,我已经检查过了。我认为问题可能是数据类型JSON,但我不确定。

    if( optimizedDatabaseLoading == 1 ){
        google.maps.event.addListener(map, 'idle', function(){
            if( searchClicked != 1 ){
                var ajaxData = {
                    optimized_loading: 1,
                    north_east_lat: map.getBounds().getNorthEast().lat(),
                    north_east_lng: map.getBounds().getNorthEast().lng(),
                    south_west_lat: map.getBounds().getSouthWest().lat(),
                    south_west_lng: map.getBounds().getSouthWest().lng(),
                };
                if( markerCluster != undefined ){
                    markerCluster.clearMarkers();
                }
                loadData(category_id, "assets/external/data.php", ajaxData);
            }
        });
    }
    else {
        google.maps.event.addListenerOnce(map, 'idle', function(){
            loadData(category_id, "assets/external/data.php");
        });
    }

    function loadData(category_id, url, ajaxData){
        console.log('inside:', category_id);
        $.ajax({
            url: url,
            dataType: "json",
            method: "POST",
            data: ajaxData, category_id: category_id,
            cache: false,
            success: function(results){
                for( var i=0; i <newMarkers.length; i++ ){
                    newMarkers[i].setMap(null);
                }
                allMarkers = results;
                placeMarkers(results);
            },
            error : function (e) {
                console.log(e);
            }
        });
    }

data.php

if (!isset($_POST['category_id'])) {
    if (!empty($_POST["optimized_loading"])) {
        $queryData = mysqli_query($connection, "SELECT id, title, latitude, longitude, address, marker_image FROM items WHERE latitude <= " . $_POST["north_east_lat"] . " AND latitude >= " . $_POST["south_west_lat"] . " AND longitude <=" . $_POST["north_east_lng"] . " AND longitude >= " . $_POST["south_west_lng"]);
    } else {
        $queryData = mysqli_query($connection, "SELECT id, title, latitude, longitude, address, marker_image FROM items");
    }
}
else {
    if( !empty( $_POST["optimized_loading"] ) ){
        $queryData = mysqli_query( $connection, "SELECT id, title, latitude, longitude, address, marker_image FROM items WHERE latitude <= " . $_POST["north_east_lat"] . " AND latitude >= " . $_POST["south_west_lat"] . " AND longitude <=" . $_POST["north_east_lng"] . " AND longitude >= " .$_POST["south_west_lng"] . " AND items.item_category_id = " . json_decode($_POST['category_id']));
    }
    else {
        $queryData = mysqli_query( $connection, "SELECT id, title, latitude, longitude, address, marker_image FROM items WHERE items.item_category_id = 1" . json_decode($_POST['category_id']) );
    }
}

1 个答案:

答案 0 :(得分:1)

您发送的数据不是您认为的,您将category_id设置为传递给$.ajax的对象的属性,而不是发送的数据。< / p>

您可以扩展对象并添加属性

$.ajax({
    url      : url,
    dataType : "json",
    method   : "POST",
    data     : $.extend(ajaxData, {category_id: category_id}),
    success  : function(results){
        for( var i=0; i <newMarkers.length; i++ ){
            newMarkers[i].setMap(null);
        }
        allMarkers = results;
        placeMarkers(results);
    },
    error   : function (e) {
        console.log(e);
    }
});