corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » php help


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author php help
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 09:46   View User's Profile U2U Member Reply With Quote

I am trying to get the distance and duration into a php variable so I can call it and display it later.

its currently in a javascript.

code:

function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;



Any ideas.

i want like $distance = the output that is produced about
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
10th Jan 08 at 09:57   View Garage View User's Profile U2U Member Reply With Quote

you want to rewrite that distance formula in php?
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 10:00   View User's Profile U2U Member Reply With Quote

I am using the google maps API and that statement produces the required output I need.

But I need to be able to call the function in php afterwards.

so i can carry the following for example.

$time = (whatever you do);
$distance = (whatever you do);

$time+$distance*100 = $price

echo $price;


So need to get the distance from the script after it is run into php.

[Edited on 10-01-2008 by Jodi_the_g]
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
10th Jan 08 at 10:00   View Garage View User's Profile U2U Member Reply With Quote

firstly you will need the form to action a php page, then the php page to get the variables from the form

something like

$distance = htmlentities($_REQUEST['distance']);
$duration = htmlentities($_REQUEST['duration']);

heres a forumla i use on vagweb

code:
$distance=sqrt(pow(($userx-$homex),2)+pow(($usery-$homey),2))/1000;
$distanceadjust = $distance * 0.19;
$distance= $distance + $distanceadjust;
$distancekm=round($distance, 0);

Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 10:05   View User's Profile U2U Member Reply With Quote

I have the form already

code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>A Class Corporate Travel</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAN7396AA-yYP5YWcjnhyRHBSPvi7yoDxBrMWr3cNIOhTzGhtskhSIzUH_dF60NfPSUgM_KJPNo6uvHg"
type="text/javascript"></script>
<style type="text/css">
body {
font-family: Verdana, Arial, sans serif;
font-size: 11px;
margin: 2px;
}
table.directions th {
background-color:#EEEEEE;
}

img {
color: #000000;
}
</style>
<script type="text/javascript">

var map;
var gdir;
var geocoder = null;
var addressMarker;
var directions;
var dis;
var distance;
var duration;
var getstartgeocode;

function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);

}
}

function setDirections(fromAddress, toAddress, en) {
gdir.load("from: " + fromAddress + ", UK" + " to: " + toAddress + ", UK",
{ "locale": en });
}

function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is incorrect, please try again.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

else alert("An unknown error occurred.");

}

function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}

<?php
$distance = distance
$duration = duration
?>

</script>




</head>
<body onload="initialize()" onunload="GUnload()">
<form action="#" onsubmit="setDirections(this.from.value, this.to.value); return false">
<div align="center">
<table>

<tr><th align="right">From:&nbsp;</th>

<td><p>
<input type="text" size="25" id="fromAddress" name="from"
value="Marlow"/>
</p> </td>
<th align="right">&nbsp;&nbsp;To:&nbsp;</th>
<td align="right"><input type="text" size="25" id="toAddress" name="to"
value="Bicester" /></td></tr>

<tr><th>
<label> </label></th>
<td colspan="3"><div align="right">
<input name="submit" type="submit" value="Submit" />
</div></td></tr>
</table>
</div>
</form>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td>
<div id="distance" align="center"></div><br />
<div id="duration" align="center"></div>
<div id="map" style="width: 0px; height: 0px; visibility:hidden" align="center"></div>
<div id="directions" style="width: 0px; heightpx; visibility:hidden" ></div>
<p align="center">
<img src="http://www.google.com/intl/en_ALL/mapfiles/poweredby.png" unselectable="on" galleryimg="no" />
</p>
</td></tr></table>
</body>
</html>


Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 10:06   View User's Profile U2U Member Reply With Quote

and it works and pulls out the information I need I just need the distance and duration valves to be stored in php after it is run, if you get me?
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
10th Jan 08 at 10:06   View Garage View User's Profile U2U Member Reply With Quote

yeah when you submit that form it calls the javascript function, if you want to send the variables to php the action="#" will need to be action="somepage.php"
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
10th Jan 08 at 10:07   View Garage View User's Profile U2U Member Reply With Quote

you might be better off putting the javascript in a php page and echo'ing it out when when you call the php page, without testing it though i dont know if that will work properly
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 10:10   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Steve
you might be better off putting the javascript in a php page and echo'ing it out when when you call the php page, without testing it though i dont know if that will work properly


thats what I am trying to do just can't figure out how to echo the output.
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 10:14   View User's Profile U2U Member Reply With Quote

I have now got index.php

code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>A Class Corporate Travel</title>

<style type="text/css">
body {
font-family: Verdana, Arial, sans serif;
font-size: 11px;
margin: 2px;
}
table.directions th {
background-color:#EEEEEE;
}

img {
color: #000000;
}
</style>

</head>
<body onload="initialize()" onunload="GUnload()">
<form action="MapAPI.class.php" onsubmit="setDirections(this.from.value, this.to.value); return false">
<div align="center">
<table>

<tr><th align="right">From:&nbsp;</th>

<td><p>
<input type="text" size="25" id="fromAddress" name="from"
value="Marlow"/>
</p> </td>
<th align="right">&nbsp;&nbsp;To:&nbsp;</th>
<td align="right"><input type="text" size="25" id="toAddress" name="to"
value="Bicester" /></td></tr>

<tr><th>
<label> </label></th>
<td colspan="3"><div align="right">
<input name="submit" type="submit" value="Submit" />
</div></td></tr>
</table>
</div>
</form>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td>
<div id="distance" align="center"></div><br />
<div id="duration" align="center"></div>
<div id="map" style="width: 0px; height: 0px; visibility:hidden" align="center"></div>
<div id="directions" style="width: 0px; heightpx; visibility:hidden" ></div>
<p align="center">
<img src="http://www.google.com/intl/en_ALL/mapfiles/poweredby.png" unselectable="on" galleryimg="no" />
</p>
</td></tr></table>
</body>
</html>



and MapAPI.class.php
code:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAN7396AA-yYP5YWcjnhyRHBSPvi7yoDxBrMWr3cNIOhTzGhtskhSIzUH_dF60NfPSUgM_KJPNo6uvHg"
type="text/javascript"></script>

<script type="text/javascript">

var map;
var gdir;
var geocoder = null;
var addressMarker;
var directions;
var dis;
var distance;
var duration;
var getstartgeocode;

function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);

}
}

function setDirections(fromAddress, toAddress, en) {
gdir.load("from: " + fromAddress + ", UK" + " to: " + toAddress + ", UK",
{ "locale": en });
}

function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is incorrect, please try again.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

else alert("An unknown error occurred.");

}

function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}



</script>



and it seems to run ok without any errors but now can not see the output.
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
10th Jan 08 at 10:14   View Garage View User's Profile U2U Member Reply With Quote

if you put the html form elements in the php page and echo them out at the same time it should work.

Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
10th Jan 08 at 10:15   View Garage View User's Profile U2U Member Reply With Quote

il have to look at this later as iv got to go and do a job now
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 10:16   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Steve
if you put the html form elements in the php page and echo them out at the same time it should work.




I confussed now.

you mean run the script seperate on sumbit then echo back?
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
10th Jan 08 at 11:35   View Garage View User's Profile U2U Member Reply With Quote

i mean output the whole page from a php echo

make the form element action the same php page and trap the variables when its submitted
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 13:19   View User's Profile U2U Member Reply With Quote

So sumbit the form to a different page with the script on then echo that whole script?
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 13:34   View User's Profile U2U Member Reply With Quote

Am trying this but it errors.
code:

<?php

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAN7396AA-yYP5YWcjnhyRHBSPvi7yoDxBrMWr3cNIOhTzGhtskhSIzUH_dF60NfPSUgM_KJPNo6uvHg"
type="text/javascript"></script>

echo <script type="text/javascript">

var map;
var gdir;
var geocoder = null;
var addressMarker;
var directions;
var dis;
var distance;
var duration;
var getstartgeocode;

function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);

}
}

function setDirections(fromAddress, toAddress, en) {
gdir.load("from: " + fromAddress + ", UK" + " to: " + toAddress + ", UK",
{ "locale": en });
}

function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is incorrect, please try again.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

else alert("An unknown error occurred.");

}

function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}


echo </script>

?>

but does not work


Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
10th Jan 08 at 22:04   View User's Profile U2U Member Reply With Quote

Pretty much tried everything now.

And still can not get the distance and duration stored in php.
drax
Member

Registered: 5th Feb 05
Location: Sittingbourne, Kent
User status: Offline
10th Jan 08 at 23:07   View User's Profile U2U Member Reply With Quote

Php has to be actioned, you cant run it inline like that, the page has to be refreshed, or the information sent to the script,

Steve already showed you this, you said you have a form, which is great but this has to send information to a php page which can take the information and send it back for the javascript to process (if you're still going to us the JS)
Jodi_the_g
Member

Registered: 7th Aug 01
Location: Washington D.C
User status: Offline
11th Jan 08 at 09:43   View User's Profile U2U Member Reply With Quote

I speratored the form from the script.

then added the following as steve suggested

code:

function onGDirectionsLoad(){
distance = gdir.getDistance();
document.getElementById("distance").innerHTML = distance.html;
duration = gdir.getDuration();
document.getElementById("duration").innerHTML = duration.html;
}

</script>

<?php
$distance = htmlentities($_REQUEST['distance']);
$duration = htmlentities($_REQUEST['duration']);
?>



but it errors in the php bit with Notice: Undefined index: duration
Dom
Member

Registered: 13th Sep 03
User status: Offline
11th Jan 08 at 12:23   View User's Profile U2U Member Reply With Quote

dude, email me the stuff and when i get in i'll have a look.


 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Few Pics from the Pod (56K Warning) PaulW General Chat 12 1058
21st Feb 05 at 16:40
by Jambo
 
Dodgy HTML/PHP/Table problem liamC Geek Day 9 778
24th Jan 07 at 19:37
by liamC
 
phpbb index Tom J Geek Day 3 765
13th Feb 07 at 15:34
by Steve
 
PHP Template Help Laney Geek Day 14 860
17th Jul 07 at 19:26
by Ian
 

Corsa Sport » Message Board » Off Day » Geek Day » php help 28 database queries in 0.0286360 seconds