OK, here is a fuller version of what you need:
<?php
function getDistance($addressFrom, $addressTo, $unit){
//Change address format
$formattedAddrFrom = str_replace(' ','+',$addressFrom);
$formattedAddrTo = str_replace(' ','+',$addressTo);
//Send request and receive json data
$geocodeFrom =
file_get_contents('http://maps.google.com/maps/api/geocode/json?
address=".$formattedAddrFrom."&sensor=false');
$outputFrom = json_decode($geocodeFrom);
$geocodeTo =
file_get_contents('http://maps.google.com/maps/api/geocode/json?
address=".$formattedAddrTo."&sensor=false');
$outputTo = json_decode($geocodeTo);
//Get latitude and longitude from geo data
$latitudeFrom = $outputFrom->results[0]->geometry->location->lat;
$longitudeFrom = $outputFrom->results[0]->geometry->location->lng;
$latitudeTo = $outputTo->results[0]->geometry->location->lat;
$longitudeTo = $outputTo->results[0]->geometry->location->lng;
//Calculate distance from latitude and longitude
$theta = $longitudeFrom - $longitudeTo;
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +
cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) *
cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.8515;
$unit = strtoupper($unit);
if ($unit == "K") {
return round($miles * 1.609344, PHP_ROUND_HALF_UP).' km';
} else if ($unit == "N") {
return round($miles * 0.8677, PHP_ROUND_HALF_UP).'';
} else {
return round ($miles).'';
}
}
$distanceTotal = null;
$addressFrom = null;
$addressTo = null;
if(!empty($_GET['addrFrom'])) { $addressFrom = $_GET['addrFrom']; }
if(!empty($_GET['addrTo'])) { $addressTo = $_GET['addrTo']; }
if(!empty($_GET['distanceTotal'])) { $distanceTotal =
$_GET['distanceTotal']; }
if (!empty($_GET['addrFrom']) && !empty($_GET['addrTo'])) {
$distanceTotal = getDistance($addressFrom, $addressTo, "N");
$distanceTotal = sprintf('%0.2f',round($distanceTotal,2));
}
$success = false;
?>
<?php
$selected='';
function get_options($select)
{
$rate=array('Economic Rate'=>3.00,'Standard Rate'=>4.00,'Express
Rate'=>5.00,'Quantumtheory'=>6.00);
$option='3.00';
foreach ($rate as $key_rate => $value_rate)
{
if($select==$value_rate)
{
$option.='<option value="'.$value_rate.'"
selected>'.$key_rate.'</option>';
}
else
{
$option.='<option value="'.$value_rate.'">'.$key_rate.'</option>';
}
}
return $option;
}
if(isset($_POST['rate']))
{
$selected=number_format((float)$_POST ['rate'], 2,'.','');
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div align="center">
<script>
if(typeof window.history.pushState == 'function') {
window.history.pushState({}, "Hide", "<?php echo
$_SERVER['PHP_SELF']; ?>");
}
</script>
<form method="get" name"lion" id"blue" action="overflow.php" >
<p class="distanceTotal">
<div id="d">
<p><b>Adress From<b>
<input type="text" name="addrFrom" id"fred" value="<?php echo $addressFrom;
?>" /></p>
<p><label>Adress To</label><b>
<input type="text" name="addrTo" id="chris" value="<?php echo $addressTo;
?>"
/></p>
<label>Total Miles<label><b>
<input type="text" id="distanceTotal" name="distanceTotal" value="<?php echo
$distanceTotal; ?>" >
<p><input type="hidden" name="success" value="false" />
<input type="submit" name="submit" value="Calculate Distance" /></p>
</div>
</p>
</form>
<form method="post" name"tiger" id="black" />
<p>
<div>
<select name="rate">
<option value="<?php echo get_options($selected); ?>" /></option>
</select>
<input type="submit" id='submit_2' name="calc_2" value="Submit" />
<input type="text" id="rateTotal" name="rat" width="110px" value="<?php echo
$selected; ?>" /><b><b><label>Per Mile</label>
</div>
</p>
</form>
</div>
</body>
</html>
14
solved How to fix information inside text box from disappearing upon submit and calculation?