I have created a page that allows a user to enter a start location and end location and the distance is output (this using a google API). I have several instances of this so effectively once the user is completed, there will be several start locations, end locations and the distance between the locations. I now need to link each ‘journey’ to a single submit form thats linked to a database using PHP however when I put this all in a form it stops working as every time the user inputs a journey the pages refreshes on itself. The only way I can see around this is putting each row in its own form and somehow linking all the forms to one submit button? How could I get around this problem or what is the best way of connecting it to a database? Sorry if I have not explained this well enough. The code for one of my rows can be seen below.
<form>
<tr height="60">
<td>Date</td>
<td>Job</td>
<td>Start Location</td>
<td>End Location</td>
<td>Confirm Route</td>
<td>Distance</td>
</tr>
<body onload="initialize(); initialize1(); initialize2(); initialize3(); initialize4(); initialize5(); initialize6(); initialize7(); initialize8(); initialize9(); initialize10(); ">
<script type="text/javascript">
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var belfast = new google.maps.LatLng(55, -5)
var myOptions = {
zoom:5,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: belfast,
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start = document.getElementById("pc1").value;
var end = document.getElementById("pc2").value;
var distanceInput = document.getElementById("distance");
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
distanceInput.value = Math.round(response.routes[0].legs[0].distance.value / 1000);
updateTotalDistance();
}
});
}
</script>
<tr height="38">
<td><input valign="top" type="text" name="date" id="datepicker" style="width:75px"/></td>
<td><textarea valign="top" name="job" cols="10" rows="1"></textarea></td>
<td><select id="pc1">
<option>Antrim Area Hospital</option>
<option>Causeway hospital</option>
<option>Braid Valley Hospital</option>
</select>
</td>
<td><select id="pc2">
<option>Antrim Area Hospital</option>
<option>Causeway hospital</option>
<option>Braid Valley Hospital</option>
</select></td>
<td><input type="submit" value="OK" name="submit" id="submit1" onclick="calcRoute()"></td>
<td><input type="text" class="distance" id="distance" readonly="true" onkeyup="return autocalc(this,t2,t3)" tabindex="1" style="width:45px"></td>
</form>
The best way to solve this would be to use AJAX techniques with PHP.