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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿