I’m calling this script using Ajax. The ‘write to database’ part of the script runs fine, but for some reason the send_receipt() function does not run when the script is run.
The send e-mail function needs to be enclosed within a custom function because the script will ultimately send two e-mails – one receipt to the customer and one notification to the company. So all of the mail() variables and data will be repeated, but with different target e-mail addresses, with both functions to be called when the script is run.
Any help much appreciated.
Thanks!
Ajax:
$.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();
For a start in your PHP you are relying on vars outside of the function scope. This is not the way PHP works. If you wish to access a variable outside of the function you have to use
global $var;to pull the variable into the function’s scope… If you do not do this, the variables would be all undefined and so yourmail()function wont know where to send what it is sending because$emailwill be empty.However, It would be far better to tailor your function so that you are sending the arguments you want to use into it – this makes it more reusable:
Or even more portable (because you can send in flexible data):
Secondly, to avoid URL encoding problems it would be best to formulate your jQuery ajax call like so:
This is because jQuery will handle URL encoding the values correctly for you, rather than what you were doing before… which had no encoding and would break the second your data contained illegal URL characters.
In response to Chris’ comment 🙂
The above wont change the way your PHP script recieves the data, it will just protect it whilst it is in transit through to your sever – and so that your PHP script can interpret the information correctly once it starts parsing it. If you don’t URL encode for example the following would break your previous URL.
Using the above URL your PHP script would most likely receive:
With regard to how you receive data on your PHP side the following would probably make it easier – although what you are doing is fine, it just isn’t using the power of server side scripting 😉