我需要传递带有id="monthlyPayment"
的div的值,它在填充js-side的输入和计算后获取值,并将其发送到电子邮件。我试图用AJAX和DOM来做这件事,但我有一点经验,所以它没有成功,也许有人可以帮助我!
function credit() {
var loanTerm = document.getElementById('loanTerm').value * 12;
var interestRate = 0.09858333;
var creditSum = document.getElementById('carPrice').value - document.getElementById('anInitialFee').value;
var monthlyPayment = (parseFloat(creditSum) * parseFloat(interestRate) / (1 - Math.pow(1 + parseFloat(interestRate), -loanTerm))).toFixed(2);
document.getElementById('monthlyPayment').innerHTML = "Ваш ежемесячный платеж: " + monthlyPayment;
document.getElementById('sent').style.visibility = "visible";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="functions.js"></script>
<script src="jquery-3.2.1.slim.js"></script>
<title></title>
</head>
<body>
<form action="mail_sender.php" method="post">
<input type="text" id="firstName" name="firstName" value="" placeholder="укажите ваше имя"><br>
<input type="email" id="email" name="email" value="" placeholder="укажите ваш email"><br>
<input type="tel" id="phone" name="phone" value="" placeholder="укажите ваш телефон"><br>
<input type="text" id="carPrice" name="carPrice" value="" placeholder="укажите цену автомобиля"><br>
<input type="text" id="anInitialFee" name="anInitialFee" value="" placeholder="укажите первоначальный взнос"><br>
<input type="text" id="loanTerm" name="loanTerm" value="" placeholder="укажите срок кредита"><br>
<div id="monthlyPayment"></div>
<button id="sent" style="visibility:hidden;">Send to email?</button>
</form>
<button id="credit" onClick="credit();">Calculate</button>
</body>
</html>
<?php
$recipient = $_POST['email'];
$subject = "Письмо с сайта";
$name = $_POST['firstName'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$carPrice = $_POST['carPrice'];
$anInitialFee = $_POST['anInitialFee'];
$loanTerm = $_POST['loanTerm'];
$monthlyPayment = $_POST['monthlyPayment'];
$mailBody = "Имя: $name \n Email: $email \n Телефон: $phone \n Цена автомобиля: $carPrice \n Первоначальный взнос: $anInitialFee \n Срок кредита: $loanTerm \n Ежемесячный платеж: ";
mail($recipient, $subject, $mailBody);
echo "Mail sent.";
?>
答案 0 :(得分:1)
monthlyPayment
是一个div元素而不是表单元素,因此它不会与$ _POST数据一起传递。
将<div>
更改为<input>
,您就可以在$_POST['monthlyPayment']
中将其提取。
在输入中添加readonly
属性,用户不会(通常)能够对其进行编辑。
答案 1 :(得分:1)
您可以将其设为隐藏的输入字段。 我想你不希望它显示给用户,因此代码将是
<input name="monthlyPayment" id="monthlyPayment" style="display: none;" disabled>
但请记住,用户始终可以修改此功能。因此,当在PHP中接收数据时,永远不会信任用户输入。