E-mail a value which is returned by a function
I was wondering if it is possible to email a value which is returned by a function in javascript? or do i have to use php/ajax?
From the following example, I want to email abc to myself? How can it be done?
<html>
<head><script>
var t = "abc";
function test(){
return t;}
</script></head>
<body onload = "test()">
</body><开发者_开发知识库;/html>
You'll need to post it to your server via XHR, then your server can email it.
Here is a very good explanation from another similar question:
You can't send an email with javascript, the closest you can get would be a mailto which opens the default email client - but that won't send anything.
Email should be sent from the server - submit the form in the normal way, and construct the email on the server and send it....
Another answer there gives some details about using mailto
.
There's no direct method. I would use jQuery' $.post to post the relevant information to a PHP page which would then mail it using, at it's simplest, the aptly-named mail function.
In one of the script tags of the main page or in a separate .js file:
$.post("mailer.php", {data: "abc"}, function(data) {
// Alert yourself whether it was successful if you want
});
And mailer.php (replace these with your own values):
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = $_POST['data'];
$headers = 'From: webmaster@example.com';
mail($to, $subject, $message, $headers);
?>
精彩评论