开发者

Disable button when clicked

As you can see in the code below, I have 2 flags my and hk.

When I click on the hk flag, it will redirect to the hk page and at the same time will disable the hk button to show the difference from other buttons.

So,what php code do I use to disable only the button that is clicked?

<button id="btn_my" dojoType="dijit.form.Button" 
  onClick='location = "<?php echo url_realurl();?>/results/view/all/M"'>
<?php 
  echo ci_create_img('my');
?> <!--this is malaysia flay image -->
</button>
<button id="btn_hk" dojoType="dijit.form.Button" 
  onClick='location = "<?php echo url_realurl();?>/results/view/all/H"'>
<?php 
  echo ci_create_img('hk');
?><!--this is hongkong开发者_开发问答 flay image-->
</button>


You could read the $_SERVER['REQUEST_URI'] and disable the button if it links to the current page. Example:

<?php
$countries = array(
    'my'    =>  '/results/view/all/M',
    'hk'    =>  '/results/view/all/H',
);
?>

<?php foreach ($countries as $code => $uri): ?>

    <?php if ($_SERVER['REQUEST_URI'] === $uri): ?>
    <button class="disabled">
    <?php else: ?>
    <button onClick='location="<?php echo url_realurl().$uri; ?>"'>
    <?php endif; ?>
        <?php echo ci_create_img($code);?>
    </button>

<?php endforeach; ?>

You might want to trim($uri, '/') and trim($_SERVER['REQUEST_URI'], '/') there to make sure there are no mistakes with slashes.

You could also append a $_GET variable to the link like ?country_code=hk and read that instead:

<?php foreach ($countries as $code => $uri): ?>

    <?php if ($_GET['country_code'] === $code): ?>
    <button class="disabled">
    <?php else: ?>
    <button onClick='location="<?php echo url_realurl().$uri.'?country_code='.$code; ?>"'>
    <?php endif; ?>
        <?php echo ci_create_img($code);?>
    </button>

<?php endforeach; ?>

There, you should also make sure to check if isset($_GET['country_code']), and any other links on the page would most likely have to use the query string as well. I was trying to make this readable. I would just write a function for reading the url personally.

I prefer the first approach. Sorry, I had to craft it in my coding style, but hopefully you get the idea and this works for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜