Rosh_LK
Rosh_LK

Reputation: 700

how to send an email to admin by running a php file through jQuery

i need to get a notification email to my site admin, when one user made a request through a from. my code is as follows, to link the php file in my server which sends the mail

$("#modelform").submit(function (event) {
        event.preventDefault();
        $.ajax({
    url: 'send_mail.php',
    success: function(){
         alert('php runing');
         $("#sendRequest").modal("show");
         $("#myModal").modal("toggle");
    }
});

    });

but it doesn't react! my knowledge is little low can anyone guide me to achieve this? i checked this question is this wrong the way i do or do i need to link any file other than bootstrap libraries?

Upvotes: 2

Views: 120

Answers (1)

Axiom
Axiom

Reputation: 902

You can try something like this:


The HTML:

<textarea id="contactUs"></textarea><div id="button">Send</div>
<div id="response"></div>

The jQuery:

$("#button").click(function(){ //when div id="button" is clicked
    var content = $("#contactUs").val(); //get value of textarea id="contactUs"
    $.post('send_mail.php',{content: content}, function(data){ //post data
        $('#response').html(data); //return content of send_mail.php
    });
});

Then the send_mail.php:

<?php 
if(isset($_POST['content']) === true){
    $content = $_POST['content']; //might wanna sanitize if you're storing into db
    $to = "[email protected]"; //The email sending to
    $subject = "Sent From Contact form"; //The subject of email
    mail($to, $subject, $content, 'From: [email protected]'); //PHP mail() function
    echo "Sent!"; //This will go to div id="response" on success
} else {
    echo "Error!"; //This will go to div id="response" on error
}
?>

Upvotes: 2

Related Questions