I am trying to create a script that will send a email to our company email when someone clicks a button. The html will be hosted on a static page and will do everything client side, whereas the php code will be hosted on another domain.
What I have tried so far is.
HTML
<head><title>Report Errors</title>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' type='text/javascript'></script></head>
<script>$(".c").click(function(){
var href = this.href;
$.ajax({
url: 'http://example.com/m35.php',
type: 'POST',
data: { subject: href },
success: function (data) {
setTimeout(function (){
$(".container").html(data)
}, 1000)
}
});
})
</script>
<a href="http://link-to-be-sent.example.com" class="c">send</a>
<div class="container">success</div>
And the code in m35.php file -
<?php
$mailTo = "mail@example.com";
$mailFrom = "no-reply@example.com";
$subject = $_POST["subject"];
$message = "someone reported the content in subject doubtful.";
mail($mailTo, $subject, $message, "From: ".$mailFrom);
?>
EDIT : Based upon comments I want to clarify that it is even not working on same domain and directory, However this type of code is also working but how to do that without page reload with javascript/jquery.
<form method="post" action="http://example.com/m35.php">
<input type="text" name="subject" id="subject" value=""> <input type="submit">
</form>