English 中文(简体)
当一个以上检查箱与j Query/AJAX和PHP检查时,我如何选择多种选择。
原标题:How do I sort multiple options when more than one checkbox is checked with jQuery/AJAX and PHP

Quick sum up: I have a facebook-like activity wall on the user profiles. There are multiple "sorting" options to choose from. So if they want to only see, say, their own activity, they click "Your Activity". That part works perfectly. I am adding a new piece to this puzzle, where they can also sort through certain types of activity such as: posts, comments, votes, etc.

我试图这样做的是,如果他们的活动已经选定,那么他们就应当只显示自己的职位和对策。

我正在使用检查箱,以便他们能够按意愿检查/检查物品,并打断一个jax/php。 这里是检查箱:

<div class="activitySelector">
    <a href="" id="stalk" class="selectOrder">Following Activity</a> &bull; <a href="" id="user" class="selectOrder">Your Activity</a>
</div>
<input type="checkbox" class="sortOption" id="post" /><label for="post">Posts</label>
<input type="checkbox" class="sortOption" id="response" /><label for="response">Responses</label>

The jQuery/ajax:

$( input[type=checkbox] ).live("click", function(){

    $( input:checked ).each(function(){
        var ID = $( .activitySelector .selected ).attr("id");
        var optionID = $(this).attr("id");

        if (optionID) {
            $( ul.streamActivity ).html( <img src="/images/ajax-loader.gif" class="loader" alt="" /> );

            $.ajax({
                type: "POST",
                url: "/profile/sort_activity.php",
                data: "sort="+ID+"&option="+optionID+"&user=<?php echo $user; ?>",
                cache: false,
                success: function(html){
                    $("ul.streamActivity").html(html);
                }
            });
        }
    });
});

最后,PHP(我只想表明与我所谈论的内容有关的法典......) 我无需表示完全的问候,因为它正在完美地工作,我只想让你能够接触到头脑:P:平常。

if ($sort == "user") {
    $checkActivity .= " WHERE a.name =  $user ";
    $activityNum .= " WHERE a.name =  $user ";

    if ($option == "post") {
        if ($option == "response") {
            $checkActivity .= " AND a.type =  Post  AND (a.type =  Comment  OR a.type =  Advice )";
            $activityNum .= " AND a.type =  Post  AND (a.type =  Comment  OR a.type =  Advice )";
        } else {
            $checkActivity .= " AND a.type =  Post ";
            $activityNum .= " AND a.type =  Post ";
        }
    } elseif ($option == "response") {
        if ($option == "post") {
            $checkActivity .= " AND a.type =  Post  AND (a.type =  Comment  OR a.type =  Advice )";
            $activityNum .= " AND a.type =  Post  AND (a.type =  Comment  OR a.type =  Advice )";
        } else {
            $checkActivity .= " AND (a.type =  Comment  OR a.type =  Advice )";
            $activityNum .= " AND (a.type =  Comment  OR a.type =  Advice )";
        }
    }
}

因此,基本上需要做的是认识到,已经选定了“Your activity”(用户),然后选择了“Posts”和“Responses”,然后只显示YOUR职位和答复。 现在,它只是显示一种或另一种(如果选择“职位”和“责任”似乎只是你的答复。 半途

我怀念也许会建立厨师,这样我会记得他们已经检查过什么,但我想尽可能避免。

如果我试图用这些冷却的新特征重新发射,将非常感激任何帮助:

UPDATE: Here is the foreach statement I ve added. Echo is in there to see if it s calling it properly, which it is. Checking on and off the boxes displays post/response/vote - it s just not displaying any results whatsoever :/

我认为,如果有一个以上选择,那么我就需要做些什么,这样它就可以选择所有选择。 似乎无法让它妥善工作。

if (isset($options)) {
foreach ($options as $option) {
    echo $option . <br /> ;
    if ($option[ post ]) {
        $checkActivity .= " AND a.type =  Post ";
        $activityNum .= " AND a.type =  Post ";
    }

    if ($option[ response ]) {
        $checkActivity .= " AND (a.type =  Comment  OR a.type =  Advice )";
        $activityNum .= " AND (a.type =  Comment  OR a.type =  Advice )";
    }

    if ($option[ vote ]) {
        $checkActivity .= " AND a.type =  Vote ";
        $activityNum .= " AND a.type =  Vote ";
    }
}
}
最佳回答

正在发生的情况是,你没有把所有支票的价值观寄给服务器。 赞成:

$( input[type=checkbox] ).live("click", function(){
    var options = []; //We ll send an array of options instead only one
    var ID = $( .activitySelector .selected ).attr("id");
    $( input:checked ).each(function(){        
        var optionID = $(this).attr("id");
        options.push("options[]="+optionID);
    });
    if (options.length) {
        $( ul.streamActivity ).html( <img src="/images/ajax-loader.gif" class="loader" alt="" /> );

        $.ajax({
            type: "POST",
            url: "/profile/sort_activity.php",
            data: "sort="+ID+"&options="+options.join("&")+"&user=<?php echo $user; ?>", //Here we re sending  options 
            cache: false,
            success: function(html){
                $("ul.streamActivity").html(html);
            }
        });
    }        
});

如你所知,我们重新发送了所有经过核对的ID s(options),而不仅仅是一个

服务器方面:

<?php
  $options = $_POST[ options ];
  //Now $options is an array with all checked IDs
  foreach($options as $option)... //Do your stuff with $options. Everything else remains the same
?>

希望这一帮助。 卡车

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签