2

I want to send an email as soon as I click on html button. To do this task, I've written following code

HTML code:

<button onclick="sendEmail()">Send Email</button>
<p id="mailStatus"></p>

Java Script Code:

function sendEmail()
{
     $.ajax({
           url: "mail.php",
           type: "POST",
           success: function(response) {
               if (!response) {
                    alert("Something went wrong. Please try again");
                    return;
               }

               var parsedJSON = eval('('+response+')');

               // If there's an error, display it.
               if(parsedJSON.Error) {
                  // Handle session timeout.
                  if (parsedJSON.Error == "Timeout") {
                       alert("Session timed out. Please login again.");
                       window.location.reload();
                   }
                }
               document.getElementById('mailStatus').innerHTML = "Email Sent successfully";  
            }
     });
}

The problem if when I click on Send Email button, I get an error message as

"Uncaught ReferenceError: $ is not defined"

Can someone please help??

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
Ravi Singh
  • 431
  • 3
  • 10
  • 23

1 Answers1

3

As $.ajax is function of jQuery so you need to add jQuery in your file.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

Add this line above sendMail function.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459