2

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rosh_LK
  • 680
  • 1
  • 15
  • 36

1 Answers1

2

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 = "YourEmail@example.com"; //The email sending to
    $subject = "Sent From Contact form"; //The subject of email
    mail($to, $subject, $content, 'From: contact@example.com'); //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
}
?>
Axiom
  • 902
  • 1
  • 10
  • 23
  • hmm so i have many fields so i need to write how? var content = ("#contactUs").val();var smthing=("#contactUS").val(); $.post('send_mail.php',{content: content,smthing}, function(data){ $('#response').html(data); //Will return content of send_mail.php }); am i correct or do i need to do any change if i am wrong sorry as i said verry little knowledge here! – Rosh_LK May 25 '15 at 03:47
  • The more fields you have, you can grab the value of them by using `var yourVariableName = $("#theIdOfYourField").val(); ` then you can post them by adding them where it has `{content:content}`. So it'd look like so: `$.post('send_mail.php', {content:content,postObject:jsVar2,etc:etc}, function(data){...` the `postObject` being what you'll access from the PHP file, and `jsVar2` being the variable you made to grab the value of the field – Axiom May 25 '15 at 03:48