0

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 Answers 2

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

your answer works fine too. So which code is slightly better? @Rakesh Sharma or Jack?
@Mike Actually, I noticed a slight logic bug in my answer, which I've updated; the difference between our answers is where the fallback value gets defined, at the top or in the else clause.
Your updated code works fine too without any php errors or notices in my error log. So, what is the difference between your code and @rakesh-sharma ?
@Mike The only difference is that my code doesn't check whether the given location by the user is non-empty, that is, is not an empty string; changing the condition to isset($_POST['location']) && strlen($_POST['location']) makes it the same thing.
I see. How would you change your above code to include sessions? So that the value in the input field will be saved when user reloads or goes to another page. Whatever they enter in there will stay there until they manually change it to something else.
|
1

try

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

1 Comment

What is the difference between your code and Jack's? Both work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.