运行脚本时未调用函数

时间:2012-10-18 07:57:20

标签: php ajax

我正在使用Ajax调用此脚本。脚本的“写入数据库”部分运行正常,但由于某种原因,脚本运行时send_receipt()函数不会运行。

发送电子邮件功能需要包含在自定义功能中,因为脚本最终会发送两封电子邮件 - 一封收据发给客户,另一封发送给公司。因此,所有的mail()变量和数据都将重复,但具有不同的目标电子邮件地址,并且在运行脚本时可以调用这两个函数。

任何帮助都非常感激。

谢谢!

的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();

2 个答案:

答案 0 :(得分:1)

要在PHP中启动,您将依赖于函数范围之外的变量。这不是PHP的工作方式。如果你想访问函数之外的变量,你必须使用global $var;将变量拉入函数的范围......如果你不这样做,变量将是未定义的,所以你的{{1函数不会知道发送内容的位置,因为mail()将为空。

$email

然而,定制你的函数以便你将要使用的参数发送到它中会好得多 - 这使得它更具可重用性:

function send_receipt(){

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

}

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

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

  /* and so on ... */

}

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

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 ... */

}

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

回应克里斯的评论:)

以上内容不会改变PHP脚本接收数据的方式,它只会在传输到服务器时保护它 - 这样一旦PHP脚本开始解析它就可以正确解释信息。例如,如果您没有URL编码,则以下内容会破坏您之前的网址。

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

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

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

关于如何在PHP端接收数据,下面的内容可能会更容易 - 虽然你做的很好,但它没有使用服务器端脚本的强大功能;)

echo $_POST['firstname'];

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

答案 1 :(得分: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);
    }
});