如何使用AJAX或JSON将数据发送到PHP电子邮件脚本

时间:2021-03-05 07:15:09

I'm completely lost here. I want to send an email with PHP without leaving my page. But I can't figure out how to get the data to the PHP file to email it. And when I do get it there, I would be lost. So any help with what the PHP file should look like would be great too. Here is the form.

我完全迷失在这里。我希望在不离开页面的情况下发送带有PHP的电子邮件。但我无法弄清楚如何将数据传送到PHP文件以通过电子邮件发送。当我确实得到它时,我会迷失方向。因此,对PHP文件应该是什么样子的任何帮助都会很棒。这是表格。

<table id="dialog">
        <tr><td><input type="text" id="name" name="name" placeholder="Your Name" size="20" class="tbow" /></td></tr>
        <tr><td><input type="text" id="email" name="email" placeholder="Your Email" size="20" class="tbow" /></td></tr>
        <tr><td><input type="text" id="number" name="number" placeholder="Your Phone Number" size="20" class="tbow" /></td></tr>
        <tr><td><textarea id="message" name="message" rows="5" cols="25" class="tbow" placeholder="Your Message"></textarea></td></tr>
        <tr><td><input type="button" value="Send!" onclick="senem();" id="sender" /></td></tr>
</table>

And I've tried this...

我试过这个......

function senem(){
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
        var number = document.getElementById("number").value;
        var message = document.getElementById("message").value;
        $.ajax({
            type: "POST",
            url: "mmaa.php",
            data: "name=" + name + "&email=" + email + "&number=" + number = "&message=" + message,
            success: alert("It works!!!");
        });
 }

2 个解决方案

#1


4  

Not intended as a copy-and-paste job, but should give you the building blocks to write it yourself:

不是作为复制粘贴工作,但应该给你自己编写的构建块:

Your javascript function;

你的javascript函数;

function senem() {
    jQuery.ajax({
        type: "POST",
        url: "http://location/of/script.php",
        data: { var1: "foo", var2: "bar" },
    });
}

and in the php;

并在PHP;

<?php
    $var1 = $_POST['var1'];
    $var2 = $_POST['var2']; //do something interesting with these

    $to = "john@example.com";
    $subject = "Wooo Email!";
    $message = "Sorry John, you're fired.";

    mail($to, $subject, $message, "From: system@yourdomain.com\r\n");
?>

The values of var1 and var2 you will probably want to assign clientside, using jQuery (eg $("#name").val(); ) and you will also need to install and configure an SMTP server alongside Apache (use Exim, and find a friendly geek to help you).

你可能想要使用jQuery(例如$(“#name”)。val();)来分配客户端的var1和var2的值,你还需要在Apache旁边安装和配置一个SMTP服务器(使用Exim,和找个友好的极客来帮助你)。

PS: the \r\n is because SMTP shares some syntax with HTTP, it's not important, but the mail command probably won't work without them.

PS:\ r \ n是因为SMTP与HTTP共享一些语法,这并不重要,但如果没有它们,mail命令可能无法运行。

#2


1  

I will remove the onlick event from button and do it an unobtrusive way

我将从按钮中删除onlick事件并以不引人注目的方式执行

<input type="button" value="Send!" id="sender" />  

Script

脚本

$(function(){

 $("#sender").click(function(){
    $.post("yourserver.php", 
                          { name: $("#name").val(),
                            email:$("#email").val(),
                            number : $("#number").val(), 
                            message=$("#message").val()
                          },
                         function(result){
                            //Show the result from server page to the user. 
                            alert(result)
                         });    
  });
});

Have your php page accepts this values and then do send the email after proper validation. You may respond some message back ( "Ex : Email sent") and that will be available in the result variable.

让您的php页面接受此值,然后在正确验证后发送电子邮件。您可以回复一些消息(“例如:发送电子邮件”),这将在结果变量中提供。

Make sure you have jQuery library loaded to your page.

确保将jQuery库加载到您的页面。

Read this pages for better understanding of how jQuery post works http://api.jquery.com/jQuery.post/

阅读本页以更好地理解jQuery post如何工作http://api.jquery.com/jQuery.post/

#1


4  

Not intended as a copy-and-paste job, but should give you the building blocks to write it yourself:

不是作为复制粘贴工作,但应该给你自己编写的构建块:

Your javascript function;

你的javascript函数;

function senem() {
    jQuery.ajax({
        type: "POST",
        url: "http://location/of/script.php",
        data: { var1: "foo", var2: "bar" },
    });
}

and in the php;

并在PHP;

<?php
    $var1 = $_POST['var1'];
    $var2 = $_POST['var2']; //do something interesting with these

    $to = "john@example.com";
    $subject = "Wooo Email!";
    $message = "Sorry John, you're fired.";

    mail($to, $subject, $message, "From: system@yourdomain.com\r\n");
?>

The values of var1 and var2 you will probably want to assign clientside, using jQuery (eg $("#name").val(); ) and you will also need to install and configure an SMTP server alongside Apache (use Exim, and find a friendly geek to help you).

你可能想要使用jQuery(例如$(“#name”)。val();)来分配客户端的var1和var2的值,你还需要在Apache旁边安装和配置一个SMTP服务器(使用Exim,和找个友好的极客来帮助你)。

PS: the \r\n is because SMTP shares some syntax with HTTP, it's not important, but the mail command probably won't work without them.

PS:\ r \ n是因为SMTP与HTTP共享一些语法,这并不重要,但如果没有它们,mail命令可能无法运行。

#2


1  

I will remove the onlick event from button and do it an unobtrusive way

我将从按钮中删除onlick事件并以不引人注目的方式执行

<input type="button" value="Send!" id="sender" />  

Script

脚本

$(function(){

 $("#sender").click(function(){
    $.post("yourserver.php", 
                          { name: $("#name").val(),
                            email:$("#email").val(),
                            number : $("#number").val(), 
                            message=$("#message").val()
                          },
                         function(result){
                            //Show the result from server page to the user. 
                            alert(result)
                         });    
  });
});

Have your php page accepts this values and then do send the email after proper validation. You may respond some message back ( "Ex : Email sent") and that will be available in the result variable.

让您的php页面接受此值,然后在正确验证后发送电子邮件。您可以回复一些消息(“例如:发送电子邮件”),这将在结果变量中提供。

Make sure you have jQuery library loaded to your page.

确保将jQuery库加载到您的页面。

Read this pages for better understanding of how jQuery post works http://api.jquery.com/jQuery.post/

阅读本页以更好地理解jQuery post如何工作http://api.jquery.com/jQuery.post/