dongpai6567 2019-06-23 15:48
浏览 57

添加第二个下拉列表以进行数据过滤的AJAX请求

I'm building a review system web application. I have a page on which I have used a drop-down menu to filter data using ajax. This code is below:

index.php (View)

Select drop-down generated by each different location in the database

<select name="retreat_locations" onchange="filterRetreats(this.value)">
    <option value="alllocations" selected="selected">All Locations</option>
    <?php foreach ($this->locations as $location) {?>
        <option value="<?=htmlentities($location->retreat_location);?>"><?=htmlentities($location->retreat_location);?> </option>
    <?php }?>
</select>

Div which displays AJAX results

<div id="display-retreats">
</div>

When drop-down selection is made, this div loads the resposne from AJAX

The AJAX Script

This script gets the selected option of the dropdown, and passes it to index/getretreat in the controller of the application. The controller uses a method in the model of the application named getRetreatsByFilter($retreat_location).

<!-- AJAX.. -->
<script>
    function filterRetreats(str) {
    if (str == "") {
        document.getElementById("display-retreats").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("display-retreats").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "index/getretreat/" + str, true);
        xmlhttp.send();
    }
}

getRetreatsByFilter($retreat_location) (Model)

/**
 * Get retreats AJAX request for sure with dropdown filter.
 * @return array an array with several objects (the results)
 * Ordered by most reviewed.
 */
public static function getRetreatsByFilter($retreat_loation)
{

    $param = $retreat_loation;

    $database = DatabaseFactory::getFactory()->getConnection();

    if($param == "alllocations") {

    $sql = "SELECT retreats.retreat_id,
                    AVG(review_total_rating) AS retreat_total_rating,
                    AVG(review_cost) AS total_review_cost, 
                    retreat_logo,
                    retreat_name,
                    retreat_website,
                    retreat_facebook,
                    retreat_instagram,
                    retreat_verified,
                    retreat_location,
                    retreat_founded_in,
                    retreat_approved,
                    count(reviews.retreat_id) as retreat_number_of_reviews
            FROM retreats
            LEFT JOIN reviews
            ON retreats.retreat_id = reviews.retreat_id
            WHERE retreat_approved = 1
            GROUP BY retreats.retreat_id
            ORDER BY count(reviews.retreat_id) DESC";

    $query = $database->prepare($sql);
    $query->execute();

    $results = $query->fetchAll();

    return $results;

    }
    else {

    $sql = "SELECT retreats.retreat_id,
                    AVG(review_total_rating) AS retreat_total_rating,
                    AVG(review_cost) AS total_review_cost, 
                    retreat_logo,
                    retreat_name,
                    retreat_website,
                    retreat_facebook,
                    retreat_instagram,
                    retreat_verified,
                    retreat_location,
                    retreat_founded_in,
                    retreat_approved,
                    count(reviews.retreat_id) as retreat_number_of_reviews
            FROM retreats
            LEFT JOIN reviews
            ON retreats.retreat_id = reviews.retreat_id
            WHERE retreat_approved = 1
            AND retreat_location = '".$retreat_loation."'
            GROUP BY retreats.retreat_id
            ORDER BY count(reviews.retreat_id) DESC";

            $query = $database->prepare($sql);
            $query->execute();

            $results = $query->fetchAll();

            return $results;

    }   

}

This data is then sent to the div on the index page:

<div id="display-retreats">
</div>

This all works as I want it to, the thing that I am really struggling with is how would I go about implementation ANOTHER select menu to filter the same set of data.. The select used at the moment is used to find location by selected drop-down value. I am trying to add another select menu to sort the data in order such as:

<label>Sort by:</label>
<select>
    <option>Age</option>
    <option>Rating</option>
    <option>Cost</option>
</select>

whilst still knowing which location was selected in the first drop-down.

If anyone has any advice on how I could implement this it would be thoroughly appreciated.

  • 写回答

1条回答 默认 最新

  • douluo7366 2019-06-23 18:32
    关注

    I would recommend posting all required parameters within a single JSON request to the server (using Ajax).

    1. Collect all form values and create an object.
    2. Convert this object to JSON and POST it via Ajax to the server
    3. On the server-side: Build and execute a dynamic SQL query with the given parameters.
    4. Render the response
    5. The client fetches the response and appends the new content to the DOM.
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么