user input - PHP if and else statement -
i have form shows users results based on location.
when page loads, php shows results based on userip location comes geoip script works fine. ip location gets stored in $location
variable.
but, want allow user change location based on zip code provides in input text field , clicks submit.
here 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 form xml loaded, &l=".$location."
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 works except when if statement fired. if zip code entered input field, reverts user based ip location.
but if user based ip location not set or "not found, not found"
else statement fires. in case user able enter zip code, click submit , see results based on zip code entered.
how can rearrange php code allow both options?
if ip location available, still want page load results, while giving user ability enter zip code , see results based on zip code provides after clicking submit.
you need rearrange based on should logically tested first:
$location = ''; // assume worst if (isset($_post['location'])) { // location given, use $location = $_post['location']; } else { // location not given, find based on ip address if possible $geo = geocheckip($_server['remote_addr']); if (isset($geo) && ($geo != "not found, not found")) { $location = $geo; } }
Comments
Post a Comment