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.