Phs发送者与js

时间:2015-05-13 12:31:24

标签: javascript php jquery

我之前从未使用过php,但我使用过jquery和js。我正在制作一个网页,我找到了我想要使用的PHP功能,从我的页面发送电子邮件:

<?php
    $to      = 'the name';
    $subject = 'the subject';
    $message = 'the message';
    $headers = 'From: Some one?' . "\r\n" .
               'Reply-To: no-reply@address.com' . "\r\n" .
               'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
?>

当我按下屏幕上的按钮时,如何启动此功能以从我的网站发送1封电子邮件。也许通过js,jquery反正..

3 个答案:

答案 0 :(得分:1)

假设php文件名为mailer.php,请使用带有jQuery的ajax

$.ajax({
      url: "mailer.php"
    });

这会向您服务器中的mailer.php发送请求。

关于按钮,您可以

<button id="MailIt">Mail</button>

然后将ajax代码放在点击处理程序中,如

$('#MailIt').click(function(){
     ajax code here
});

答案 1 :(得分:1)

您可以通过添加到网址将变量传递给php。我们可以在Jscript中更改window.location。

说你要通过发送“到”和“主题”。 您需要将网址从www.example.com/mailer.php更改为

  

'url':'www.example.com/mailer.php?to = user @ example.com&amp ;subject = hello'

然后你需要改变php来接受这些变量。 你可以先检查一下:

if(isset($_GET['to'])
    $to = $_GET['to'];
if(isset($_GET['subject'])
    $subject = $_GET['subject'];
//...
//etc

答案 2 :(得分:0)

您必须将HTTP请求发送到PHP代码的URL。

相关问题