doucan8049 2014-06-26 07:18 采纳率: 100%
浏览 37
已采纳

PHP if和else语句

I have a form that shows users results based on location.

When the page loads, the PHP shows the results based on the userIP location which comes from my geoIP script which works fine. This ip location gets stored in the $location variable.

But, What I also want is to allow the user to change this location based on what zip code he provides in the input text field and then clicks submit.

Here is my PHP:

$geo = geoCheckIP($_SERVER['REMOTE_ADDR']);

if (isset($geo) && ($geo != "not found, not found")) {
    $location = $geo;
}
    else
    {
    $location = (isset($_POST['location'])    ? $_POST['location']    : '');
}

The form:

<form action="/" method="POST">
<div class="postal"><input name="location" id="postal" type="text" value="<?php echo $location; ?>" placeholder="Postal Code or City, State"></div><div id="example">ex.: San Francisco, CA</div><div class="search"><input name="search" id="search" type="submit" value="Search"></div>
</form>

Then below the form is where the xml is loaded, where &l=".$location." is where the zip code gets inserted.

xml:

$xml = simplexml_load_file($url."publisher=".$publisher."&q=".$q."&l=".$location."&sort=".$sort."&radius=".$radius."&st=".$st."&jt=".$jt."&start=".$start."&limit=".$limit."&fromage=".$fromage."&highlight=".$highlight."&filter=".$filter."&latlong=".$latlong."&co=".$co."&chnl=".$chnl."&userip=".$userip."&useragent=".$useragent."&v=".$v);

So everything works except for when the if statement is fired. If a zip code is entered into the input field, it just reverts back to the user based IP Location.

But if the User based IP location is not set or "not found, not found" then the else statement fires. In that case the user is able to enter a zip code, click submit and see results based on the zip code entered.

How can I rearrange my php code to allow both options?

If the IP location is available, I still want the page to initially load with those results, while giving the user the ability to enter zip code and see results based on what zip code he provides after clicking submit.

  • 写回答

2条回答 默认 最新

  • duan198811 2014-06-26 07:21
    关注

    You just need to rearrange based on what should logically be tested first:

    $location = ''; // assume the worst
    if (isset($_POST['location'])) {
        // location given, use that
        $location = $_POST['location'];
    } else {
        // location not given, find it based on IP address if possible
        $geo = geoCheckIP($_SERVER['REMOTE_ADDR']);
    
        if (isset($geo) && ($geo != "not found, not found")) {
            $location = $geo;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用