2013-01-09 41 views
-2

我遇到以下代碼的問題。它只是阻止網站的其他部分加載,並沒有達到預期的功能。加載PHP代碼阻止網站內容

有一個JS的主要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('store/indexMain.php?'+this.name+'=' + boxes.join("+"), 
      function() { 
       $(".indexMain").fadeIn('slow'); 
       $(".loadingItems").fadeOut(300); 
      }); 

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

<?php 
function echoCheckboxSet($con, $header, $divClass, $columnName, $setName) { 
$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"); 
?> 

Warning: Missing argument 5 for echoCheckboxSet(), called in 
\store\indexLeftBar.php on line 58 and defined in \store\indexLeftBar.php on line 35 

    Fatal error: Call to a member function prepare() on a non-object in \store\indexLeftBar.php on line 36 

你看到的任何問題?

謝謝!

+4

您是如何解決這個問題的? –

+0

就像約翰問的那樣,不知道你已經做了什麼步驟來向我們展示問題的可能性,這真的很難回答你。試着做一些打印語句來跟蹤代碼執行的流程。說「沒有任何事情發生」不會幫助我們。 – DaOgre

+0

此代碼工作之前,我把它運行到我的父目錄的子文件夾中的PHP文件。有一個Javascript在此之前運行,它也工作正常。 – samyb8

回答

3
$con

被使用,但不能在範圍:

function echoCheckboxSet($header, $divClass, $columnName, $setName) { 
    $checkboxes = $con->.... 
} 

無論是在將它作爲一個參數(如下所示)的功能,或聲明爲global(後者不推薦)。

function echoCheckboxSet($con, $header, $divClass, $columnName, $setName) { 
    $checkboxes = $con->.... 
} 

$con = some_method_that_connects_to_your_db(); // <!-- Still need this 
echoCheckBoxSet($con, "COLOR", "colors", "color_base1", "color"); 
+0

我應該如何將它作爲參數傳遞? – samyb8

+0

@ samyb8 - 我用一個例子更新了我的答案。 – nickb

+0

我看不出爲什麼我的原始代碼不起作用。我認爲你的回答沒有解決問題 – samyb8