我正在使用 Ajax 调用此脚本。脚本的“写入数据库”部分运行良好,但由于某种原因,脚本运行时 send_receipt() 函数没有运行。
发送电子邮件函数需要包含在自定义函数中,因为脚本最终将发送两封电子邮件——一封给客户的收据和一封给公司的通知。因此,所有的 mail() 变量和数据都将重复,但目标电子邮件地址不同,脚本运行时会调用这两个函数。
非常感谢任何帮助。
谢谢!
阿贾克斯:
$.ajax({
type: "POST",
url: "selfnormal.php",
data: "fname="+ fname +"& lname="+ lname +"& worktel="+ worktel +"& privtel="+ privtel +"& mobtel="+ mobtel +"& email="+ email +"& workaddress="+ workaddress +"& homeaddress="+ homeaddress +"& level="+ level +"& size="+ size +"& deliv="+ deliv +"& venue="+ venue +"& ioshdate="+ ioshdate +"& isdiscount="+ isdiscount +"& elcas="+ elcas +"& funding="+ funding +"& paytype="+ paytype +"& selfinvoicead="+ selfinvoicead +"& companyname="+ companyname +"& companyaddress="+ companyaddress +"& PO="+ PO +"& tcs="+ tcs +"& finalprice="+ finalprice +"& finalvat="+ finalvat +"& finalfee="+ finalfee +"& finaltotal="+ finaltotal +"& finalmonthly="+ finalmonthly +"& finaladmin="+ finaladmin,
success: function(){
alert ("success");
}
PHP:
<?php
$firstname = htmlspecialchars(trim($_POST['fname']));
$lastname = htmlspecialchars(trim($_POST['lname']));
$worktel = htmlspecialchars(trim($_POST['worktel']));
$privtel = htmlspecialchars(trim($_POST['privtel']));
$mobtel = htmlspecialchars(trim($_POST['mobtel']));
$email = htmlspecialchars(trim($_POST['email']));
$workaddress = htmlspecialchars(trim($_POST['workaddress']));
$homeaddress = htmlspecialchars(trim($_POST['homeaddress']));
$level = htmlspecialchars(trim($_POST['level']));
$size = htmlspecialchars(trim($_POST['size']));
$deliv = htmlspecialchars(trim($_POST['deliv']));
$venue = htmlspecialchars(trim($_POST['venue']));
$ioshdate = htmlspecialchars(trim($_POST['ioshdate']));
$isdiscount = htmlspecialchars(trim($_POST['isdiscount']));
$elcas = htmlspecialchars(trim($_POST['elcas']));
$funding = htmlspecialchars(trim($_POST['funding']));
$paytype = htmlspecialchars(trim($_POST['paytype']));
$selfinvoicead = htmlspecialchars(trim($_POST['selfinvoicead']));
$companyname = htmlspecialchars(trim($_POST['companyname']));
$companyaddress = htmlspecialchars(trim($_POST['companyaddress']));
$po = htmlspecialchars(trim($_POST['PO']));
$tcs = htmlspecialchars(trim($_POST['tcs']));
$courseprice = htmlspecialchars(trim($_POST['finalprice']));
$vat = htmlspecialchars(trim($_POST['finalvat']));
$fee = htmlspecialchars(trim($_POST['finalfee']));
$admin = htmlspecialchars(trim($_POST['finaladmin']));
$total = htmlspecialchars(trim($_POST['finaltotal']));
$monthly = htmlspecialchars(trim($_POST['finalmonthly']));
$dbc = mysqli_connect('xxxx', 'xxxx', 'xxxx', 'xxxx')
or die ('Could not connect to MySQL server.');
$query = "INSERT INTO enrolments (fname, lname, worktel, privtel, mobtel, email, workaddress, homeaddress, level, size, deliv, venue, ioshdate, isdiscount, elcas, funding, paytype, selfinvoicead, companyname, companyaddress, po, tcs, price, VAT, BIFM_Fee, Total, Monthly, adminfee)" .
"VALUES ('$firstname', '$lastname', '$worktel', '$privtel', '$mobtel', '$email', '$workaddress', '$homeaddress', '$level', '$size','$deliv','$venue', '$ioshdate','$isdiscount','$elcas', '$funding', '$paytype','$selfinvoicead','$companyname','$companyaddress','$po','$tcs', '$courseprice', '$vat', '$fee', '$total', '$monthly', '$admin')";
$result = mysqli_query($dbc, $query)
or die ('error querying database');
mysqli_close($dbc);
function send_receipt() {
$to = $email;
$subject = $firstname . ', thank you for enrolling on the ' . $level . ' ' . $size . ' (' . $deliv . ')';
$msg = "Hi $firstname," . PHP_EOL .
PHP_EOL .
"Thanks for enrolling with us. Please find a summary of your enrolment below. We'll be in touch shortly to arrange payment, after which we will send you joining instructions and course details." . PHP_EOL .
PHP_EOL .
"Please be aware that in accordance with UK Legislation, you are legally entitled to a 7 day 'cooling off' period during which you may cancel your course at no cost. After this period, you will be liable for full payment as detailed below." . PHP_EOL .
PHP_EOL .
"Level: $level" . PHP_EOL .
"Scale: $size" . PHP_EOL .
"Delivery Method: $deliv" . PHP_EOL .
"Payment Method: $paytype" . PHP_EOL .
"Course Price: £$courseprice" . PHP_EOL .
"VAT: £$vat" . PHP_EOL .
"ILM/BIFM fee: £$fee" . PHP_EOL .
"Total: £$total" . PHP_EOL .
PHP_EOL .
"We look forward to welcoming you onto the course in the near future." . PHP_EOL .
PHP_EOL .
"Kind regards" . PHP_EOL .
PHP_EOL .
"The Xenon Group staff";
mail ($to, $subject, $msg, 'From: Xenon Group Enrolments');
}
send_receipt();