Page 1 of 1

Complex form with variables

Posted: Fri Sep 18, 2009 3:40 pm
by PHPeanuts
Hello guys,

I'm working actually for a limousines site, where the final client rent a ride with the super-cars.

The thing is that in the main Page I need a complex form, where the user will fill a "From" field and a "To" field, and select number of passengers (which would affect to the final price, as a variable).

The resulting page should print the kilometers and price, as well as a button storing the price as a value to send to PayPal.

The starting and ending points fields would use Google Maps to process and provide the covered distance as seen here: http://briancray.com/2009/06/23/calcula ... -maps-api/

Then I should take that distance, and multiply it by 100$ (100$ per Km for example).

And, if there was several passengers, add 100$ per extra passenger.

Problem : I never tried to do something similar. I know how to retrieve data from a database with PHP (I work usually with WordPress), but there, I'm a bit lost.

Do you think is easy ? There's some script avaliable that I should check, having a similar way of work ? The Google API is working for me, but I'm unable to mix a variable there, or turn Km in $...

Thanks for any tip,

Re: Complex form with variables

Posted: Fri Sep 18, 2009 3:42 pm
by John Cartwright
It would be helpful to post what you have tried so far.

Re: Complex form with variables

Posted: Fri Sep 18, 2009 4:08 pm
by PHPeanuts
Hi John, thanks this is what I have by now

In the 'head' section there's the Google javascript :

Code: Select all

 
<script type="text/javascript">
 
    var geocoder, location1, location2, gDir;
 
    function initialize() {
        geocoder = new GClientGeocoder();
        gDir = new GDirections();
        GEvent.addListener(gDir, "load", function() {
            var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
            var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
            document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)';
        });
    }
 
    function showLocation() {
        geocoder.getLocations(document.forms[0].address1.value, function (response) {
            if (!response || response.Status.code != 200)
            {
                alert("Sorry, we were unable to geocode the first address");
            }
            else
            {
                location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                geocoder.getLocations(document.forms[0].address2.value, function (response) {
                    if (!response || response.Status.code != 200)
                    {
                        alert("Sorry, we were unable to geocode the second address");
                    }
                    else
                    {
                        location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                        gDir.load('from: ' + location1.address + ' to: ' + location2.address);
                    }
                });
            }
        });
    }
 
</script>
 

In the body section the form, where the 'p' with id=results will output the kilometers :

Code: Select all

 
<body onload="initialize()">
 
    <form action="#" onsubmit="showLocation(); return false;">
        <p>
            <input type="text" name="address1" value="Address 1" />
            <input type="text" name="address2" value="Address 2" />
            <input type="submit" value="Search" />
        </p>
    </form>
    <p id="results"></p>
 
</body>
 
I'm able that way to print the distance in Km between A and B, provided by the user (and always they are real adresses), thanks to Google processing those adresses.

But the question is, how can I multiply that result to print as well the price (1Km being 100$) ?
And, can I add a variable like 1 extra passenger = +100$ to that ?


Thanks very much.

Re: Complex form with variables

Posted: Fri Sep 18, 2009 4:53 pm
by John Cartwright
I would probably create a dropdown for the number of passengers, then it's a simple matter of collecting the values and performing the calculation. Something along the lines of ..

Code: Select all

var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
var costPerKilometer = 100;
var costPerPassenger = 100;
var numPassengers = ParseInt(document.forms[0].numpassengers.value);
 
//every km = 100$ + 100 extra for each passenger
var totalCost = (drivingDistanceKilometers * costPerKilometer) + (numPassengers * costPerPassenger );
 
document.getElementById('results').innerHTML = ' .... Total Cost: '+ totalCost)';
 
 
 
Moved to Javascript.