2012-02-25 86 views
0

對於編程我很新穎,我已經使下面的代碼顯示了li 元素中的鏈接,如果滿足php條件的話。一切都工作正常,但問題是,像li這樣的html元素不會在滿足php條件時刷新並需要刷新頁面。我知道這是因爲服務器和客戶端語言。 有人可以幫助我解決這個問題的ajax代碼建議。當滿足php條件時刷新ul或div元素

大大appriciated,THX

<!-- checks if product in cart and if yes shows checkout and cart button --> 
    <ul class="main_menu">                   
    <?php if ($this->cart->hasProducts()) { ?> 
    <li><a id="top-checkout" href="<?php echo $checkout; ?>"><?php echo $text_checkout; ?></a></li> 
    <li><a id="top-cart" href="<?php echo $cart; ?>"><?php echo $text_cart; ?></a></li> 

    <?php } else { ?> 
<!-- checks if voucher in cart and if yes shows checkout and cart button --> 

    <?php if ($this->cart->hasProducts() || (isset($this->session->data['vouchers']) && $this->session->data['vouchers'])) { ?> 
     <li><a id="top-checkout" href="<?php echo $checkout; ?>"><?php echo $text_checkout; ?></a></li> 
     <li><a id="top-cart" href="<?php echo $cart; ?>"><?php echo $text_cart; ?></a></li> 
    <?php } ?> 

<?php } ?> 

<!--  END  --> 
<!-- checks if customer is logged in and if yes shows account button --> 
     <?php if ($logged) { ?> 
     <li><a id="top-account" href="<?php echo $account; ?>"><?php echo $text_account; ?></a></li> 
    <?php } ?> 
<!--  END  --> 

回答

0

比方說,你是<li></li>元素在<ul></ul>items的ID(怪異ID,但它會爲例子做的),如果你

然後「再使用JQuery做到這一點:

$(document).ready(function() 
{ 
    $.ajax({ 
     url: "showListItems.php", 
     success: function(dataReceived) { 
     $("#items").html(dataReceived); 
     } 
    }); 
} 
}); 

在你showListItems.php文件,補充一點:

if ($this->cart->hasProducts()) { 
    echo "<li><a id='top-checkout' href='".$checkout."'>".$text_checkout."</a></li>"; 
    echo "<li><a id='top-cart' href='".$cart."'>".$text_cart."</a></li>"; 

} else { 
    if (
      $this->cart->hasProducts() || 
      (
      isset($this->session->data['vouchers']) && 
      $this->session->data['vouchers'] 
      ) 
     ) 
    { 
     echo "<li><a id='top-checkout' href='".$checkout."'>".$text_checkout."</a></li>"; 
     echo "<li><a id='top-cart' href='".$cart."'>".$text_cart."</a></li>"; 
    } 
} 

if ($logged) { 
    echo "<li><a id='top-account' href='".$account."'>".$text_account."</a></li>"; 
} 

這樣,每當html頁面負載,根據某些條件,<li></li>元件被打印。此外,是否有任何事件要處理,如點擊或鼠標懸停?

如果我的代碼有錯誤(我確信它有一些錯誤),有人可以提醒我嗎?如果我的代碼是完全是不對,告訴我。否則,測試它並享受!