PHP乘法和减法脚本

时间:2015-06-14 17:58:46

标签: javascript php html css

我正在尝试制作一个PHP脚本,用于计算汽车租赁客户驾驶的总里程(每英里0.12美分),将该值乘以他们租车的天数(每天15美元)并显示在textarea框中的总计及其姓名和地址。我是相当新的PHP,我不知道如何将这个想法投射到PHP代码。我有一个完整的html版本的代码工作,但缺乏知识与PHP翻译它。

任何人对我如何编写此脚本有任何想法?

<?php
if(isset($_POST['submit'])) {
$x = $_POST['bOdometer'];
$y = $_POST['eOdometer'];
$z = $_POST['daysRented'];
$miles = $y - $x; {
$result = (15 * $z) + ($miles * 0.12);
echo $result; } }
?>
<body>
<div align="center">
<hr>
<br>
<form method="post" name id="Main">
<input type="text" id="name" name="customerName" placeholder="Enter your name here" size="30px">   
<br><br>
<input type="text" id="address" name="customerAddress" placeholder="Enter your street address here" size="50px">   
<br><br>
<input type="text" id="city" name="customerCity" placeholder="What city do you live in?" size="30px">   
<br><br>
<input type="number" id="zip" name="customerZip" placeholder="Enter your zip code" size="30px">   
<br><br>
<input type="number" id="bOdometer" name="beginningOdometerReading" placeholder="Start odometer reading" size="80px">   
<br><br>
<input type="number" id="eOdometer" name="endingOdometerReading" placeholder="End odometer reading" width="80px"> 
<br><br>
<input type="number" id="daysRented" name="endingOdometerReading" placeholder="Days rented" size="50px">  
<br><br>
<input type="button" id="total" value="Calculate how many miles you drove and your total cost!" onclick="javascript:multiply();"/>
<br><br>
Miles Driven: <input type="number" id='miles' min="1" max"10000" readonly="" />
Total Cost: <input type="number" id='result' min="1" max"10000" readonly="" />
<br><br>
<input type="button" value="Generate Summary!" onclick="javascript:calculate();"/>
<br><br>
Summary: <textarea cols="30" rows="2" id="textarea"> </textarea>
<br><br>
<input type="reset" value="Reset">
</form>       
<hr>
</div>
</body>

以下是此问题的HTML版本。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function calculate() {    
var customerName = Main.name.value;
var customerAdd = Main.address.value;
var totalCost = document.getElementById('result').value;
var area = document.getElementById("textarea");
area.value='Name: '+customerName+'\nAddress: '+customerAdd+'\nTotal Cost: $'+totalCost+'';}
</script> 

<script type="text/javascript">
function multiply() {
var b = document.getElementById('bOdometer').value;
var e = document.getElementById('eOdometer').value;
var result = document.getElementById('miles');
var myResult = (e - b);
result.value= myResult;
var d = document.getElementById('daysRented').value;
var m = document.getElementById('miles').value;
var result2 = document.getElementById('result');
var myResult2 = (d * 15 + m * 0.12);
result2.value= myResult2;
}
</script>

</head>
<body>
<div align="center">
<hr>
<br>
<form name id="Main">
<input type="text" id="name" name="customerName" placeholder="Enter your name here" size="30px">   
<br><br>
<input type="text" id="address" name="customerAddress" placeholder="Enter your street address here" size="50px">   
<br><br>
<input type="text" id="city" name="customerCity" placeholder="What city do you live in?" size="30px">   
<br><br>
<input type="number" id="zip" name="customerZip" placeholder="Enter your zip code" size="30px">   
<br><br>
<input type="number" id="bOdometer" name="beginningOdometerReading" placeholder="Start odometer reading" size="80px">   
<br><br>
<input type="number" id="eOdometer" name="endingOdometerReading" placeholder="End odometer reading" width="80px"> 
<br><br>
<input type="number" id="daysRented" name="endingOdometerReading" placeholder="Days rented" size="50px">  
<br><br>
<input type="button" id="total" value="Calculate how many miles you drove and your total cost!" onclick="javascript:multiply();"/>
<br><br>
Miles Driven: <input type="number" id='miles' min="1" max"10000" readonly="" />
Total Cost: <input type="number" id='result' min="1" max"10000" readonly="" />
<br><br>
<input type="button" value="Generate Summary!" onclick="javascript:calculate();"/>
<br><br>
Summary: <textarea cols="30" rows="2" id="textarea"> </textarea>
<br><br>
<input type="reset" value="Reset">
</form>       
<hr>
</div>
</body>

2 个答案:

答案 0 :(得分:1)

您还没有添加提交按钮。 取代

input type="button" value="Generate Summary!" onclick="javascript:calculate();"/>

使用

input type="submit" value="Calculate " />

答案 1 :(得分:0)

<?php
if(isset($_POST['summary'])) 
{
$x = $_POST['bOdometer'];
$y = $_POST['eOdometer'];
$z = $_POST['daysRented'];
$name=$_POST["customerName"];
$address=$_POST["customerAdd"];
$city=$_POST["customerCity"];
$zip=$_POST["customerZip"];



$miles = $y - $x; 
$result = (15 * $z) + ($miles * 0.12);
$summary= 'Name: '.$name. 'Address:'.$address.'Cost'.$result;

?>
<body>
<div align="center">
<hr>
<br>

 <form method="post">
<input type="text" id="name" name="customerName" value ="<?php echo $name;?>" size="30px">
<br><br>
<input type="text" id="address" name="customerAddress" value ="<?php echo $address;?>"size="50px">  
<br><br>
<input type="text" id="city" name="customerCity" value = "<?php echo $city;?>" size="30px">
<br><br>
<input type="number" id="zip" name="customerZip" value = "<?php echo $zip;?>" size="30px">
<br><br>
<input type="number" id="bOdometer" name="bOdometer" value ="<?php echo $x;?>"  size="80px">
<br><br>
<input type="number" id="eOdometer" name="eOdometer" value ="<?php echo $y;?>" width="80px">
<br><br>
<input type="number" id="daysRented" name="daysRented" value="<?php echo $z;?>" size="50px">  
<br><br>
<input type="button" id="total"  value="Calculate how many miles you drove and your total cost!" onclick="javascript:multiply();"/>
<br><br>
Miles Driven: <input type="number" id="miles"  name ="customerAdd"  min="1" max"10000" readonly="" />
Total Cost: <input type="number" id= "result" name ="totalCost" min="1" max"10000" readonly="" />
<br><br>
<input type="submit"  name ="summary" value="Generate Summary!" onclick="javascript:calculate();"/>
<br><br>

Summary: <textarea cols="30" rows="2" id="textarea" name ="txt"  ><?php echo $summary;?></textarea> 
<br><br>
<input type="reset" value="Reset">
</form>   
<hr>
</div>
</body>
<?php
} 
else
{
?>
<body>
<div align="center">
<hr>
<br>
<form method="post" name id="Main">
<input type="text" id="name" name="customerName" placeholder="Enter your name here" size="30px">   
<br><br>
<input type="text" id="address" name="customerAddress" placeholder="Enter your street address here" size="50px">   
<br><br>
<input type="text" id="city" name="customerCity" placeholder="What city do you live in?" size="30px">   
<br><br>
<input type="number" id="zip" name="customerZip" placeholder="Enter your zip code" size="30px">   
<br><br>
<input type="number" id="bOdometer" name="bOdometer" placeholder="Start odometer reading" size="80px">   
<br><br>
<input type="number" id="eOdometer" name="eOdometer" placeholder="End odometer reading" width="80px"> 
<br><br>
<input type="number" id="daysRented" name="daysRented" placeholder="Days rented" size="50px">  
<br><br>
<input type="button" id="total"  value="Calculate how many miles you drove and your total cost!" onclick="javascript:multiply();"/>
<br><br>
Miles Driven: <input type="number" id='miles'  name ="customerAdd"  min="1" max"10000" readonly="" />
Total Cost: <input type="number" id='result'  name ="totalCost" min="1" max"10000" readonly="" />
<br><br>
<input type="submit"  name ="summary" value="Generate Summary!" onclick="javascript:calculate();"/>
<br><br>
Summary: <textarea cols="30" rows="2" id="textarea"> </textarea>
<br><br>
<input type="reset" value="Reset">
</form>       
<hr>
</div>
</body>
<?php 
}
?>

几个指针 请为您输入的每个html控件命名,以便通过$ _POST []

进行检索