Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选中两个复选框时未进入正确的循环_Javascript_Php_Html_Checkbox - Fatal编程技术网

Javascript 选中两个复选框时未进入正确的循环

Javascript 选中两个复选框时未进入正确的循环,javascript,php,html,checkbox,Javascript,Php,Html,Checkbox,我有一个代码,其中我有两个类别的复选框,一个是品牌,另一个是折扣百分比。根据这些复选框,div被过滤。div的过滤正在进行,但有一个小问题。 下面是第一个PHP页面 <html> <head> <title>Insert title here</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script

我有一个代码,其中我有两个类别的复选框,一个是品牌,另一个是折扣百分比。根据这些复选框,div被过滤。div的过滤正在进行,但有一个小问题。 下面是第一个PHP页面

<html>
<head>
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style type="text/css">
#image{
width:250px;
height:250px;
border:1px solid black;
}
</style>
</head>
<body>

<script type="text/javascript">
    function get_check_value() {
        var c_value = [];

        $('input[name="brand"]:checked').each(function () {
            c_value.push(this.value);
        });
        return c_value.join(',');

    }
    function get_disc_value(){
    var d_value=[];
        $('input[name="discount"]:checked').each(function () {
            d_value.push(this.value);
        });
        return d_value.join(',');
        }

    $(document).ready(function(){
   checkboxValidate = function (e) {
   if(e)e.preventDefault();
    alert("hi");
        //var os = $('#originState').val();
       //var c = $('#commodity').val();
        //var ds = $('#destState').val();
        var ser = get_check_value();
        var disc=get_disc_value();
        //var queryString = "os=" + os;
        var data = "?ser=" + ser;
        var queryString = "&ser=" + ser;
       // alert(ser);
       $.ajax({
       //alert("ajax");
        type: "POST",
        url: "sortingajax.php",
        data: {ser:ser,disc:disc},
        dataType :  'html',
        success: function (b) {
           // alert(a+' ok. '+b)
            $('#results').html(b);
            console.log(b);
        }
    });

    } 
    $( "[type=checkbox]" ).change(checkboxValidate);

    checkboxValidate();
});
</script>


brand
    <input type="checkbox" name="brand" value="Sunbaby" id="check" />Sunbaby
    <br/>
    <input type="checkbox" name="brand" value="Advance Baby" id="check"/>Advance Baby
    <br/>
    store
    <br/>
    <input type="checkbox" name="discount" value="10" />10
    <br/>
    <input type="checkbox" name="discount" value="20" />20
    <br/>
    <input type="checkbox" name="discount" value="30" />30
    <br/>


<button id="btnSubmit">sort</button>
<div id="image">
<img src="http://img5a.flixcart.com/image/sunglass/4/u/y/mb-d4-09b-miami-blues-free-size-275x275-imadzkhuchryqjgp.jpeg" width="250px" height="250px"/>
</div>
<div id="results">
sdfsdfsdfsdfdsfgsdgsbsfgvf
</div>
</body>
</html>
Asortingajax.php—我在其中检查数据库

<?php

include('connection.php');
$query=$_POST['ser'];

$query2=$_POST['disc'];


$query=explode(",",$query);
$query = array_filter($query);
$query2=explode(",",$query2);
$query2 = array_filter($query2);
$result=count($query);
$result1=count($query1);
//echo $result;
echo $query;
echo $query1;
echo $result1;
$parts = array();
$brandarray=array();
$discarray=array();
$limit = 10;
    $offset = 0;
    foreach( $query as $queryword ){
    $brandarray[] = '`BRAND` LIKE "%'.$queryword.'%"';

}
foreach( $query2 as $discword ){
    $discarray[] = '`DPERCENT` < '.$discword.'';

}
if(!empty($query) && !empty($query2))
{ 
echo "both loops";
$countsql2='SELECT * FROM xml WHERE  ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') ';
print($countsql2);
    $combinesql=mysql_query($countsql2);
    $androws123 = mysql_num_rows($combinesql);  
$countArray1=array();
echo "<br />";
echo $androws123;
$totalrows=0;
$orsqlrows=0;
while($row = mysql_fetch_array($countsql3)) {
    // Append to the array


    $countArray1[] = $row;



    //echo $row['PID']."<BR />";
}
if(empty($countArray1))
{
echo "or";
    $orsql='SELECT * FROM xml WHERE  ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') ';
    $orsql1=mysql_query($orsql);
    $orsqlrows = mysql_num_rows($orsql1);   
    $countArray2=array();
echo $orsqlrows;
while($row = mysql_fetch_array($countsql1)) {
    // Append to the array


    $countArray2[] = $row;



    //echo $row['PID']."<BR />";
}
}
$totalrows=$orsqlrows+$androws123;
echo $orsqlrows;
echo "hi";
echo $androws123;

if($totalrows==$androws123)
{
echo "and";
foreach( $brandcheck as $queryword ){
    $brandarray[] = '`BRAND` LIKE "%'.$queryword.'%"';

}

$brandsql='SELECT * FROM XML WHERE ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') limit '.$offset.', '.$limit.' ';
    $brandsql1=mysql_query($brandsql);
$numrows = mysql_num_rows($brandsql1);  
$countArray=array();
//print($brandsql);
echo "<br />";
while($row = mysql_fetch_array($brandsql1)) {
    // Append to the array


    $countArray[] = $row;



    //echo $row['PID']."<BR />";
}

}
else{
foreach( $brandcheck as $queryword ){
    $brandarray[] = '`BRAND` LIKE "%'.$queryword.'%"';

}
echo "orloop";
$brandsql='SELECT * FROM XML WHERE  ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') limit '.$offset.', '.$limit.' ';
//print($brandsql);
    $brandsql1=mysql_query($brandsql);
$numrows = mysql_num_rows($brandsql1);  
$countArray=array();
//print($brandsql);
echo "<br />";
echo $numrows;
while($row = mysql_fetch_array($brandsql1)) {
    // Append to the array


    $countArray[] = $row;



    //echo $row['PID']."<BR />";
}

}

}
?>


<?php
foreach($countArray as $array)
{
?>

<div>
<a href="index23.php?name=<?php $array['IMAGEURL']?>"><img src="<?php echo $array['IMAGEURL']?>"/></a></div>

<?php $i++; } ?>
在第一个php页面中。当我一次选中一个品牌复选框和一个折扣复选框时,意味着同时选中了品牌和折扣中的一个。div s被正确过滤。但是当我选中折扣复选框中的两个复选框和品牌中的一个复选框时,我没有正确过滤div s,而div s应该被过滤


帮帮我,我在上面的代码中哪里做错了…

您是否回显了查询以查看您得到了什么?在我看来,只要没有使用“或”的实际内爆“胶水”,它就可以正常工作。我想你需要把胶水部分改成“或”,注意它周围的空间