1

我正在使用 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();
4

2 回答 2

1

首先,您的 PHP 依赖于函数范围之外的 var。这不是 PHP 的工作方式。如果您希望访问函数外部的变量,则必须使用global $var;将变量拉入函数的范围...如果您不这样做,则变量将全部未定义,因此您的mail()函数将不知道将其发送到哪里正在发送,因为$email将为空。

function send_receipt(){

  global $email, $firstname, $lastname; /* and so on... */

}

但是,最好定制您的函数,以便您将要使用的参数发送到其中 - 这使得它更可重用:

function send_receipt( $email, $firstname, $lastname ){

  /* and so on ... */

}

甚至更便携(因为您可以发送灵活的数据):

function send_receipt( $email_to, $user_data ){

  /// place any default values here - just in case a value is missed
  $user_data += array(
    'firstname' => '',
    'lastname' => '',
  );

  /// create a shortcut to the data
  $ud = $user_data;

  $msg  = "Hi {$ud[firstname]}," . PHP_EOL .
          "Thanks for enrolling with us....";

  /* and so on ... */

}

其次,为了避免 URL 编码问题,最好像这样制定 jQuery ajax 调用:

$.ajax({
    type: "POST",
    url: "selfnormal.php",
    data: {
      "fname": fname,
      "lname": lname,
      "worktel": worktel,
      /* and so on */
    },
    success: function(){
     alert ("success");
    }
 });

这是因为 jQuery 将为您正确处理 URL 编码值,而不是您之前所做的......它没有编码并且会破坏您的数据包含非法 URL 字符的第二个。

回应克里斯的评论:)

上述内容不会改变您的 PHP 脚本接收数据的方式,它只会在数据传输到您的服务器时对其进行保护 - 以便您的 PHP 脚本在开始解析信息后可以正确解释信息。例如,如果您不进行 URL 编码,则以下内容会破坏您之前的 URL。

var firstname = 'Pebbl & Pebbl';
var lastname = 'Pebbl';
var url = 'firstname=' + firstname + '&lastname=' + lastname;

使用上面的 URL,您的 PHP 脚本很可能会收到:

echo $_POST['firstname'];

/// would output 'Pebbl ' rather than the correct 'Pebbl & Pebbl'

关于您如何在 PHP 端接收数据,以下可能会使它更容易 - 尽管您所做的很好,但它只是没有使用服务器端脚本的功能;)

/// define array of 'allowed param names' and 'var names to use in your script'
$allowed_fields = array(
  'fname' => 'firstname',
  'lname' => 'lastname',
  'email' => 'email',
  /* and so on ...*/
);

/// step each of the allowed fields
foreach( $allowed_fields as $field_name => $script_name ){

  /// check the post array to see if we have a value for that param name
  if ( !empty($_POST[$field_name]) ) {
    /// if we do, process by removing whitespace
    $value = trim( $_POST[$field_name] );
    /// and converting html to safe characters
    $value = htmlspecialchars( $value );
  }
  else {
    /// if no value default to empty
    $value = '';
  }

  /// use a variable variable to set the variable defined by $script_name 
  /// to $value i.e. if $script_name == 'email', then this would be the 
  /// same as writing $email = $value;
  $$script_name = $value;

}
于 2012-10-18T08:02:55.543 回答
0

尝试修改您的 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");
    },
    error: function (xhr, status, thrownError) {
       alert(xhr.responseText);
       alert(thrownError);
    }
});
于 2012-10-18T08:06:26.647 回答