PHP car rental calculator, no clue how to convert it from html to php!

I am attempting to make a php script that will calculate the total miles driven by a car rental customer (.12 cents per mile) which multiplies that value to the number of days they rented the car ($15 a day) and display that grand total in a textarea box along with their name and address. I am fairly new to php and I have no clue how to project this idea into php code. I have a fully html version of this code working, but am lacking the knowledge with php to translate it.

Anyone have any ideas on how I can write this script?

<?php if(isset($_POST['submit'])) { $x = $_POST['bOdometer']; $y = $_POST['eOdometer']; $z = $_POST['daysRented']; $miles = $y - $x; { $result = (15 * $z) + ($miles * 0.12); $message = $_POST['textarea']; echo $result + $message; } } ?>


















Miles Driven: Total Cost:

Summary:


HTML VERSION FOR REFERENCE



















Miles Driven: Total Cost:



Summary:


I am attempting to make a php script that will calculate the total miles driven by a car rental customer (.12 cents per mile) which multiplies that value to the number of days they rented the car ($15 a day) and display that grand total in a textarea box along with their name and address. I am fairly new to php and I have no clue how to project this idea into php code. I have a fully html version of this code working, but am lacking the knowledge with php to translate it.

Anyone have any ideas on how I can write this script?

[php]

<?php if(isset($_POST['submit'])) { $x = $_POST['bOdometer']; $y = $_POST['eOdometer']; $z = $_POST['daysRented']; $miles = $y - $x; { $result = (15 * $z) + ($miles * 0.12); $message = $_POST['textarea']; echo $result + $message; } } ?>


















Miles Driven: Total Cost:

Summary:


[/php] WORKING HTML VERSION FOR REFERENCE [php]


















Miles Driven: Total Cost:



Summary:


[/php]

You have some errors in your code for starters, that are not related to PHP.

Advice, name your variables with meaningful names. $x says nothing about what it is or what values it holds. Whereas, $Odometer says what it should be holding.

What issue are you having? I don’t want to tear into code without knowing what you are expecting to happen and where you think it fails.

The problem is that when the submit button is pushed, no result is displayed. It literally only clears the form like a reset button would. How would you solve this problem?

Two options for displaying result:

  1. Echo output outside of the text box
  2. Add PHP inside HTML and Echo out values within the HTML tags

I’m only going to give you hints in the right direction, because I think you are trying to figure this out.
First, you have some brackets that don’t make any sense in you php block. second run the code below where yours is currently.

[php]

<?php if(isset($_POST['submit'])) { $x = $_POST['bOdometer']; $y = $_POST['eOdometer']; $z = $_POST['daysRented']; $miles = $y - $x; $result = (15 * $z) + ($miles * 0.12); $message = $_POST['textarea']; echo $result + $message; } else echo "Submit not sent


"; ?>

[/php]

And add this just under the opening <?php for good measure. You are misnaming several variables,

[php]print_r( $_POST );[/php]

Trying to figure out my next move, I’ve been stumped on this problem for quite some time now. Turned to you guys in search of answers so I can better understand how PHP works with these types of variables and inputs.

It’s actually a pretty simple mistake, after adding the print_r( $_POST) function, it should show where a grievous error was made. Hopefully, you know where to rectify it in your form.

I am missing the hint. Where is this simple mistake that I am missing?

Look at your $_POST variable names and then the names for your form fields. Notice anything?

$_POST variable names are the names, not the IDs correct? Is that it? Is that why? I put the IDs instead of the names. This helps me to stay on the road to fixing this problem, but still has not ultimately solved the problem of the results not displaying. Here’s what the code looks like now.

[php]

<?php if(isset($_POST['submit'])) { $x = $_POST['beginningOdometerReading']; $y = $_POST['endingOdometerReading']; $z = $_POST['daysRentedCar']; $miles = $y - $x; $result = (15 * $z) + ($miles * 0.12); $message = $_POST['textarea']; echo $result + $message; } ?> CS310: Project 2 #4

Jamie's Car Rental Calculator!



















Miles Driven: Total Cost:

Summary:


Created by: Jamie McGraw 6/15/15

[/php]

What do you think this line does?

[php] $message = $_POST[‘textarea’];[/php]

Explain it to me.

You also REALLY need to fix your html. The document is incorrect and if you were my student I would dock you for it.

Sponsor our Newsletter | Privacy Policy | Terms of Service