我对编程很陌生,我制作了下面的代码来显示li中的链接元素,如果php条件得到满足。目前一切都很好,但问题是当php条件满足时,像li这样的html元素不会刷新,并且需要页面刷新。我知道这是因为服务器和客户端的语言。有人能帮我提供一个ajax代码建议来解决这个问题吗。
非常有见地,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 -->
假设您是<li></li>
元素,它们位于id为items
的<ul></ul>
中(奇怪的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>
元素。此外,有没有你想处理的事件,比如点击或鼠标悬停?
如果我的代码有错误(我相信它有一些错误),有人能提醒我吗?如果我的代码完全不正确,请告诉我。否则,请测试并享受!