Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8702597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:41:31+00:00 2026-06-13T02:41:31+00:00

I’m calling this script using Ajax. The ‘write to database’ part of the script

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:41:33+00:00Added an answer on June 13, 2026 at 2:41 am

    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 your mail() function wont know where to send what it is sending because $email will be empty.

    function send_receipt(){
    
      global $email, $firstname, $lastname; /* and so on... */
    
    }
    

    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:

    function send_receipt( $email, $firstname, $lastname ){
    
      /* and so on ... */
    
    }
    

    Or even more portable (because you can send in flexible data):

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

    Secondly, to avoid URL encoding problems it would be best to formulate your jQuery ajax call like so:

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

    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.

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

    Using the above URL your PHP script would most likely receive:

    echo $_POST['firstname'];
    
    /// would output 'Pebbl ' rather than the correct 'Pebbl & Pebbl'
    

    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 😉

    /// 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;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.