douzhu3367 2018-05-07 13:36
浏览 67
已采纳

国家代码隐藏的div

I'm not a php-hero so, I try to hidden a section if the user not come from a specific country.

So I did this:

$.get("http://ipinfo.io", function (response) {
    $("#country").html(response.country);
}, "jsonp");

<input hidden id="country" type="text" name="country" value="">

This work well and show me the country code (eg. IT). Now I try to get this value and insert in a IF

$country = $_GET['country'];
$it = "IT";

<?php if ($country != $it): ?>
    Code to hidden here...
<?php endif; ?>

What is it wrong here?

  • 写回答

3条回答 默认 最新

  • douju1365 2018-05-07 13:42
    关注

    Change

    $("#country").html(response.country);
    

    to

    $("#country").val(response.country);
    

    Because php $_GET saves values.

    Also I do not see a reason to do this:

    $it = "IT";
    <?php if ($country != $it): ?>
    

    You can just do

    <?php if ($country != "IT"): ?>
    

    And last but not least you should not access $_GET directly. It is better to use function filter_input which in your case would be filter_input(INPUT_GET, 'country')

    EDIT

    I do not understand what is the hidden input for. But if you want to show or hide content depending on the country, and you get the country using ajax there is absolutely no need for this input.

    Instead of making php condition (<?php if ($country != "IT")...) You can do it in js. Let's say that inside your condition there is a div with class content

    Solution

    Your html would look more or less like this

    <div class="content">
        <!-- Your content here -->
    </div>
    

    instead of php condition.

    And in js you can do something like this

    $.get("http://ipinfo.io", function (response) {
        if (response.country == "IT") {
            $(".content").hide();
        }
    }, "jsonp");
    

    So what do we do here?

    We check if country code equals "IT". If it is true we hide the content. And this is the same what you were doing in php (if country different than IT show content).

    EDIT 2

    Instead of hiding the div you can remove it

    $(".content").remove();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?