2012-12-26 65 views
0

我很努力與下面的PHP和JavaScript代碼有2套複選框篩選從MySQL數據庫中獲得的數據範圍。問題與複選框和Javascript,PHP

下面是代碼:

<script type="text/javascript"> 
//http://jsbin.com/ujuse/1/edit 
$(function() { 
    $("input[type='checkbox']").on('change', function() { 
     var boxes = []; 
     // You could save a little time and space by doing this: 
     var name = this.name; 
     // critical change on next line 
     $("input[type='checkbox'][name='"+this.name+"']:checked").each(function() { 
      boxes.push(this.value); 
     }); 
     if (boxes.length) { 
      $(".loadingItems").fadeIn(300); 
      // Change the name here as well 
      $(".indexMain").load('indexMain.php?'+this.name+'=' + boxes.join("+"), 
      function() { 
       $(".indexMain").fadeIn('slow'); 
       $(".loadingItems").fadeOut(300); 
      }); 

     } else { 
      $(".loadingItems").fadeIn(300); 
      $(".indexMain").load('indexMain.php', function() { 
       $(".indexMain").fadeIn('slow'); 
       $(".loadingItems").fadeOut(300); 
      }); 
     } 
    }); 
}); 
</script> 


<?php 
function echoCheckboxSet($header, $divClass, $columnName, $setName) { 

    include ("connection.php"); 
$checkboxes = $con -> prepare("SELECT DISTINCT $columnName FROM item_descr ORDER BY $columnName ASC"); 
$checkboxes->execute(); 
?> 
<div class="bgFilterTitles"> 
    <h1 class="filterTitles"><?php echo $header;?></h1> 
</div> 
<div class="<?php echo $divClass; ?>"> 
<?php 
    while ($box = $checkboxes->fetch(PDO::FETCH_ASSOC)): 
    $boxColumnName = str_replace('_',' ',$box[$columnName]); 
?> 
     <input type='checkbox' class='regularCheckbox' name='<?php echo $setName; ?>' value='<?php echo $box[$columnName]; ?>' /> 
     <font class='similarItemsText'><?php echo $boxColumnName; ?></font> 
     <br /> 
<?php 
endwhile; 
?> 
</div> 
<?php 
} // end of echoCheckboxSet 

// Call our method twice, once for colors and once for prices 
echoCheckBoxSet("COLOR", "colors", "color_base1", "color[]"); 
echoCheckBoxSet("PRICE", "prices", "price", "price[]"); 
?> 

那麼我完全得到我的複選框,但對其中的任何點擊時,他們沒有做任何事情。

indexMain.php檢索值這樣的:

$colors = $_GET['color[]']; 
echo "TEST".$colors[1]; 
      $colors = explode(' ', $colors); 
      $parameters = join(', ', array_fill(0, count($colors), '?')); 
      $items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 IN ({$parameters})"); 
      $items ->execute($colors); 
      $count = $items -> rowCount(); 

-----------------添加回聲:

echo "<div>Showing ".$count."items</div>"; 
while($info = $items->fetch(PDO::FETCH_ASSOC)) 
{ 
echo "<div name='item' id='".$info['color_base1']."' class='itemBox'><div class='showItem'><a href='items_descr.php?itemId=".$info[id_item]."'><img class='itemImage' alt='' src='images/$info[imageMid].jpg'></img></div><br />"; 
echo "<div class='indexItemText'><font class='similarItemsText'><a href='items_descr.php?itemId=".$info[id_item]."'>".$info[name]."</a><font class='price'> - $".$info[price]."</div></div>"; 
$row_count++; 
if ($row_count % 2 == 0) 
    { 
echo "<br />"; // close the row if we're on an even record 
    } 

} 

的任何想法可能會發生什麼?

+0

你想在你的頁面輸出什麼? –

+3

嘗試做一個'print_r($ _ GET)'PHP在'inputs []''上將自動格式轉換爲數組。你也應該使用'include_once()',你每次調用這個函數時都要包含連接參數。 – Touki

+0

在檢查兩個方框(布朗和灰色)後執行print_r時,我得到:'Array([color] => Array([0] => Brown Gray))' – samyb8

回答

2

問題是,當你在你的JS功能構建查詢:

'indexMain.php?'+this.name+'=' + boxes.join("+") 

這將color[]=Brown+Grey,而不是color[]=Brown&amp;color[]=Grey。正確的(但髒)的方式是這樣的:

'indexMain.php?'+this.name+'=' + boxes.join('&amp;' + this.name + '=') 

你可以嘗試使用jQuery.param()(http://api.jquery.com/jQuery.param/),以獲得一個更好的代碼。

另外,在PHP中,複選框值在數組$_GET['color'](不是$_GET['color[]'])中可用。

編輯:對不起,看得太快。

答:正如你希望到處使用字符串,使用color而不是color[]在你的JS和PHP代碼。

+0

謝謝,我改變了$ _GET,現在我看到值已收到好,但我仍然無法顯示我的項目。每次我點擊第一組複選框中的一個值(一種顏色)時,屏幕將根本沒有任何項目重新加載。 – samyb8

+0

當你說屏幕時,你是指所有的頁面,還是你的容器'.indexMain'? – tmuguet

+0

我的意思是我的容器。它按預期的方式重新加載,但它不顯示任何項目(即使我期待所有顏色爲棕色或灰色的項目......) – samyb8